我正在制作一个名为foo的列表,其中存储了n个随机值。我希望能够创建一个循环,不断地添加随机值,并将新值存储在foo[I]中,这是到目前为止我的代码。任何时候我运行这个我都无法退出while循环。你知道吗
import random
foo=[]
i=0
flag = False
print("How many horses do you want to race?")
horses = eval(input())
print (horses)
while flag == False:
for i in range(horses):
foo.insert(i, random.randrange(4,41))
print(a)
if foo[i] >= 5280:
flag = True
else:
i=i+1
我认为这不起作用的原因是因为我实际上没有添加到行中foo[I]中存储的值
foo.insert(i, random.randrange(4,41))
但我不知道该怎么办。谢谢你的帮助!你知道吗
您可以完全避免
foo
上的显式循环。你知道吗另外,如果调用变量
horse_pos
而不是foo
,事情会更容易理解。我希望你会添加一个动画显示步骤后,每匹马的位置更新!:)您可能要将其更改为:
一:
a
变量i
变量取出(您不应该这样做)flag == False
换成not flag:
Don't compare boolean values to True or False using == .
相关问题 更多 >
编程相关推荐