我正在尝试做的是从变量“FGlasgow”中获取食物并将其添加到变量“食物”中,这很好而且简单,但我注意到即使 FGlasgow 变为负数,脚本仍然需要更多,所以我告诉脚本如果Glasgow < 0 添加食物并取一个随机数,问题是这是否可以缩短,以及我的方法是否正确。
import random
def RandomNo():
Random = random.randint(0,50)
return Random
Food = 1
FGlasgow = 100
while True:
Random = RandomNo()
Food += Random
FGlasgow -= Random
while FGlasgow < 0:
Food -= Random
FGlasgow += Random
Random = RandomNo()
Food += Random
FGlasgow -= Random
print "You have found" , Random , "units of food"
感谢您的帮助:)任何建议都会很棒:)