我需要打印20次

时间:2015-04-24 23:03:12

标签: python random

所以基本上我在这里有我的代码,通过3个列表打印一个随机的莎士比亚侮辱。要提交此代码,我必须打印20次侮辱,我知道有一种比写最终打印语句20次更快的方法。这可能是补救,但我不记得该怎么做。这是我的代码谢谢你们:

import random

list1 = ["Artless", "Bawdy", "Bootless", "Churlish", "Clouted"]
list2 = ["Base-court", "Bat-fowling", "Beetle-headed", "Clay-brained" ]
list3 = ["Apple-john", "Baggage", "Bladder", "Boar-pig", "Coxcomb"]

def nurd1():
  return (random.choice(list1))

def nurd2():
   return (random.choice(list2))

def nurd3(): 
    return (random.choice(list3))

print ("Thou" + " " + nurd1() + " " +  nurd2() + " " + nurd3() )

3 个答案:

答案 0 :(得分:1)

import random

words = [
["Thou"],
["Artless", "Bawdy", "Bootless", "Churlish", "Clouted"],
["Base-court", "Bat-fowling", "Beetle-headed", "Clay-brained" ],
["Apple-john", "Baggage", "Bladder", "Boar-pig", "Coxcomb"]
]

print(*(' '.join([random.choice(l) for l in words]) for r in range(20)), sep='\n')

答案 1 :(得分:0)

from random import choice
list =[
 ["Artless", "Bawdy", "Bootless", "Churlish", "Clouted"],
 ["Base-court", "Bat-fowling", "Beetle-headed", "Clay-brained" ],
 ["Apple-john", "Baggage", "Bladder", "Boar-pig", "Coxcomb"]
]
for i in range(20):
    print '\nThou ',
    for j in range(len(list)):
        print choice(list[j]),

答案 2 :(得分:0)

简单循环

loop = 0

while(loop <= 19):
    print ("Thou" + " " + nurd1() + " " +  nurd2() + " " + nurd3() )
    loop += 1

tho'它会打印20个不同的行!