需要帮助,让随机生成的数字在执行5次后放入列表,最好是通过while循环。当我需要5时,我的代码只打印一个数字。这就是我到目前为止所拥有的,加上我对python相当陌生,所以需要一些技巧。
import random
die1 = random.randint(1,6)
result = [ ]
rolls = 0
while rolls < 5:
ran_die = random.randint(1,6)
result = ran_die
rolls = rolls + 1
print(result)
#I want the end results to appear list this. Thank you for help
[1, 2, 3, 4, 5]
您需要在生成时附加列表。
import random
result = [ ]
rolls = 0
while rolls < 5:
result.append(random.randint(1,6))
rolls = rolls + 1
print(result)
更好的方法是:
import random
results = [random.randint(1,0) for x in range(5)]