为什么我的Python脚本没有运行它在Space=5时应该运行的代码?
为什么我的Python脚本没有运行它在Space=5时应该运行的代码 我希望它在空间达到5时运行此代码:为什么我的Python脚本没有运行它在Space=5时应该运行的代码?,python,Python,为什么我的Python脚本没有运行它在Space=5时应该运行的代码 我希望它在空间达到5时运行此代码: print ("Ladder!") Space = 16 print ("You're not on space " + str(Space) + "!") 但它没有,只是转到下一个转弯处 完整代码为: import time Space = 0 Turn = 0 while Space < 100: Turn = Turn + 1 print ("Turn "
print ("Ladder!")
Space = 16
print ("You're not on space " + str(Space) + "!")
但它没有,只是转到下一个转弯处
完整代码为:
import time
Space = 0
Turn = 0
while Space < 100:
Turn = Turn + 1
print ("Turn " + str(Turn) + ":\n")
DiceRoll = random.randint(1,6)
print ("Dice Roll Number: " + str (DiceRoll))
Space = Space + DiceRoll
print ("Space: " + str(Space))
print ("\n")
time.sleep (2)
if (Space) > 99:
print ("You win!")
if (Space) == 5:
print ("Ladder!")
Space = 16
print ("You're now on space " + str(Space) + "!")
在退出while循环之前,您的条件不会应用。如果要检查条件,则需要将其缩进,使其位于while循环内。因为If语句位于循环外,并且在循环后,您至少要将其空格设置为100。因此,当if语句运行时,如果您的if语句不在while循环中,那么空间就永远没有机会变为5。如果您的if语句不在while循环中,那么空间将一直增加到99,循环存在,只有到那时您的if语句才会被计算。您能提供更多关于您预期会发生什么以及出现什么错误的详细信息吗?从空格=0开始,然后输入while Sapce<100。因此,当您完成循环并达到if条件时,空间wc不能等于5@xibalba1非常感谢。