Python3因为有限的知识代码赢得了';行不通
当前代码:Python3因为有限的知识代码赢得了';行不通,python,python-3.x,Python,Python 3.x,当前代码: import random numbers=[] for i in range(20): spam = random.randint(1,30) print(spam) 我想将垃圾邮件插入数字中,但这就是我被卡住的地方 预期结果: import random numbers=[] for i in range(20): spam = random.randint(1,30) print(spam) 20个随机数的列表使用此代码 import ran
import random
numbers=[]
for i in range(20):
spam = random.randint(1,30)
print(spam)
我想将垃圾邮件
插入数字
中,但这就是我被卡住的地方
预期结果:
import random
numbers=[]
for i in range(20):
spam = random.randint(1,30)
print(spam)
20个随机数的列表使用此代码
import random
numbers=[]
for i in range(20):
spam = random.randint(1,30)
numbers.append(spam)
print numbers
输出
[14, 19, 5, 20, 17, 8, 7, 28, 18, 3, 26, 9, 10, 15, 28, 20, 8, 26, 13, 16]
您的可能不同,因为它们是随机数使用此代码
import random
numbers=[]
for i in range(20):
spam = random.randint(1,30)
numbers.append(spam)
print numbers
输出
[14, 19, 5, 20, 17, 8, 7, 28, 18, 3, 26, 9, 10, 15, 28, 20, 8, 26, 13, 16]
您的可能会有所不同,因为它们是随机数您就快到了,但您需要将随机数附加到列表中,而不是只打印随机数。只需添加行
数字。将(垃圾邮件)
附加到for
循环的正文中
(如果不再需要,可以删除打印语句。)
有更优雅的方法来构建此列表(请参见列表理解答案),但在您的级别
append
很好。您已经差不多做到了,但您需要将其添加到列表number
中,而不仅仅是打印随机数。只需添加行数字。将(垃圾邮件)
附加到for
循环的正文中
(如果不再需要,可以删除打印语句。)
有更优雅的方法来构建此列表(请参见列表理解答案),但在您的级别上,
append
也可以。或者您可以使用列表理解:
numbers = [random.randint(1, 30) for _ in range(20)]
或者,您可以使用列表理解:
numbers = [random.randint(1, 30) for _ in range(20)]
numbers.append(spam)
在您的for
循环中就是您要查找的内容。您的列表是numbers
,而不是spam
。你是说你没有找到任何关于如何用python填写列表的指南?numbers。在for
循环中追加(垃圾邮件)
就是你要找的。你的列表是numbers
,而不是spam
。你是说你没有找到任何关于如何用python填充列表的指南?