英文:
while loop wont stop running in python
问题
我正在编写一个Python中的崩溃游戏,我试图让它在数值达到1、50或100时崩溃,但它一直继续下去。有人知道原因吗?
import random
import time
game = 1
num = 1.00
while game == 1:
num += .01
print(round(num, 2))
time.sleep(.1)
if game != 1:
print("Crash!")
break
while game == 1:
crash = random.randint(1, 100)
time.sleep(.1)
if crash == 1 or crash == 50 or crash == 100:
game -= 1
else:
break
这是您提供的Python代码的翻译。
英文:
I am working on a crash game in python and im trying to make it crash when a value hits 1,50, or 100 but it keeps going. Does anyone know why?
import random
import time
game = 1
num = 1.00
while game == 1:
num += .01
print(round(num, 2))
time.sleep(.1)
if game != 1:
print("Crash!")
break
while game == 1:
crash = random.randint(1, 100)
time.sleep(.1)
if crash == 1 or crash == 50 or crash == 100:
game -= 1
else:
break
答案1
得分: 3
在第一个 while 循环中,游戏的值从未从1更改,因此永远不会退出第一个 while 循环。
顺便说一下,你不必打破循环,因为一旦 game 不等于1,它就不会再次进入循环。
我认为你的意思是只使用你编写的第二个 while 循环 - 所以你可以删除第一个循环,并从第二个循环中去掉 else - 没有必要,因为你不需要在第一个循环中打破它的同样的原因。
所以,如果我正确理解你的意图,代码应该是:
import random
import time
game = 1
while game == 1:
crash = random.randint(1, 100)
time.sleep(.1)
if crash == 1 or crash == 50 or crash == 100:
game -= 1
英文:
In first while loop game's value never changes from 1 therefore never exits the first while loop
BTW you do not have to break the loop because once game != 1 it will won't enter the loop again.
I think you mean to use only the second while loop you have written - so you can delete the first one and remove the else from the second - no need for it for the same reason you do not need the break in the first loop.
so if I understand your intention correctly than the code should be:
import time
game = 1
while game == 1:
crash = random.randint(1, 100)
time.sleep(.1)
if crash == 1 or crash == 50 or crash == 100:
game -= 1
答案2
得分: 1
如果你对计算机科学不太了解,有时在查看代码时模拟一下"扮演计算机"可能会有帮助。看看代码的每一步,并尝试想象会发生什么。在像Python这样的语言中,甚至可以逐个测试这些语句,以确保它们执行你认为的操作。
让我们跳到重要的部分。
内存状态是游戏和num都等于1。
while game == 1: # 循环直到game不等于1
num += .01 # 将0.01添加到num
print(round(num, 2)) # 打印 round(num, 2); 第一次是1.01
time.sleep(.1) # 休眠1/10秒
if game != 1: # 在此之前未更改game,所以game=1
print("Crash!") # 这将永远不会执行,忽略它。
break
# 循环结束:执行流程再次返回到开头,num=1.01
你看到问题了吗?这永远不会退出,因为在循环中game永远不会改变,它将永远继续循环。第二个循环甚至不会被执行,因为它将一直卡在第一个循环中。
英文:
If you're new to computer science, it can sometimes be useful to "play computer" when looking at code. Look at each step of the code and try to imagine what would happen. In a language like python, you can even test these statements one at a time to make sure they do what you think they do.
Let's skip to the important part.
The state of the memory is game and num both equal 1.
while game == 1: # loop until game is not 1
num += .01 # add 0.01 to num
print(round(num, 2)) #print round(num, 2); 1.01 the first time
time.sleep(.1) # sleep for 1 10th of a second
if game != 1: #game was not changed before this point, so game=1
print("Crash!") # this will never execute, ignore it.
break
# End of the loop: execution flow return to the start again, with num=1.01
You see the problem? This will never exit because game is never altered in the loop, this will continue to loop forever. The second loop won't ever even be reached because it's going to be stuck in the first one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论