Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我需要把这个打印20次_Python_Random - Fatal编程技术网

Python 我需要把这个打印20次

Python 我需要把这个打印20次,python,random,Python,Random,所以基本上我这里有我的代码,它通过3个列表随机打印出莎士比亚的侮辱。要提交这段代码,我必须打印20次侮辱,我知道有一种比写20次最终打印语句更快的方法。这可能是补救措施,但我不记得怎么做了。这是我的代码谢谢各位: import random list1 = ["Artless", "Bawdy", "Bootless", "Churlish", "Clouted"] list2 = ["Base-court", "Bat-fowling", "Beetle-headed", "Clay-bra

所以基本上我这里有我的代码,它通过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() )
简单while循环

loop = 0

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

虽然(loop looping ring a bell?@TigerhawkT3 meh,它可以是一行,因为所有这些函数都是从列表中
返回
一个随机选择的元素,而
random.choice()
已经做到了这一点,我认为这些函数唯一的一点就是学习如何使用函数…在这种情况下,它们的定义很差(最好的定义是
random.choice()
使用的定义,带有参数)。这是最基本的。我希望这有助于隐藏内置列表类型。换行符不是必需的,我怀疑OP使用的是Python 3.x,其中print是一个函数而不是一个语句。我还想知道第一行的分号在那里做什么。哈哈,哇,一分钟前我在做javascript,我的b公元
loop = 0

while(loop <= 19):
    print ("Thou" + " " + nurd1() + " " +  nurd2() + " " + nurd3() )
    loop += 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')