虽然循环没有中断(Python)
While Loop not breaking (Python)
我以前使用过 while 循环之类的,但是这个根本达不到中断条件。这个游戏是关于在盒子里找到隐藏的东西。我已经放置了一些实际游戏中不会出现的代码,可帮助我验证隐藏了哪个框。盒子的范围是从 1 到 5,每次重新开始游戏时都是随机的。我从猜测框开始,因为我需要一些东西来填充 space 并将 in_box 变成一个字符串以防万一。
from random import randrange
in_box = randrange(1, 5)
str(in_box)
guess_box = False
print("To guess which box enter in the numbers that each box relates to, eg, Box 1 will be the number 1! Ready? Set? Go!")
while guess_box != in_box:
print(f"I was in box {in_box}")
guess_box = input("Which box? ")
if guess_box == in_box:
print("Great job, you found me!")
break
else:
print("I'm still hiding!!")
print("Thank you for playing")
您需要将输入转换为整数类型,input() 的默认类型是 str.
结果是像 '1' == 1 这样的逻辑,它是假的,所以条件永远不会通过。
from random import randrange
in_box = randrange(1, 5)
str(in_box)
guess_box = False
print("To guess which box enter in the numbers that each box relates to, eg, Box 1 will be the number 1! Ready? Set? Go!")
while guess_box != in_box:
print(f"I was in box {in_box}")
guess_box = input("Which box? ")
if int(guess_box) == in_box:
print("Great job, you found me!")
break
else:
print("I'm still hiding!!")
print("Thank you for playing")
有效,注意 if 条件下猜测框周围的 int()。
您正在将 in_box 设置为一个字符串并且没有保存它。你需要做 in_box=str(in_box)
:
from random import randrange
in_box = randrange(1, 5)
in_box = str(in_box)
guess_box = False
print("To guess which box enter in the numbers that each box relates to, eg, Box 1 will be the number 1! Ready? Set? Go!")
while guess_box != in_box:
print(f"I was in box {in_box}")
guess_box = input("Which box? ")
if guess_box == in_box:
print("Great job, you found me!")
break
else:
print("I'm still hiding!!")
print("Thank you for playing")
否则,永远不会满足打破循环的条件。
我以前使用过 while 循环之类的,但是这个根本达不到中断条件。这个游戏是关于在盒子里找到隐藏的东西。我已经放置了一些实际游戏中不会出现的代码,可帮助我验证隐藏了哪个框。盒子的范围是从 1 到 5,每次重新开始游戏时都是随机的。我从猜测框开始,因为我需要一些东西来填充 space 并将 in_box 变成一个字符串以防万一。
from random import randrange
in_box = randrange(1, 5)
str(in_box)
guess_box = False
print("To guess which box enter in the numbers that each box relates to, eg, Box 1 will be the number 1! Ready? Set? Go!")
while guess_box != in_box:
print(f"I was in box {in_box}")
guess_box = input("Which box? ")
if guess_box == in_box:
print("Great job, you found me!")
break
else:
print("I'm still hiding!!")
print("Thank you for playing")
您需要将输入转换为整数类型,input() 的默认类型是 str.
结果是像 '1' == 1 这样的逻辑,它是假的,所以条件永远不会通过。
from random import randrange
in_box = randrange(1, 5)
str(in_box)
guess_box = False
print("To guess which box enter in the numbers that each box relates to, eg, Box 1 will be the number 1! Ready? Set? Go!")
while guess_box != in_box:
print(f"I was in box {in_box}")
guess_box = input("Which box? ")
if int(guess_box) == in_box:
print("Great job, you found me!")
break
else:
print("I'm still hiding!!")
print("Thank you for playing")
有效,注意 if 条件下猜测框周围的 int()。
您正在将 in_box 设置为一个字符串并且没有保存它。你需要做 in_box=str(in_box)
:
from random import randrange
in_box = randrange(1, 5)
in_box = str(in_box)
guess_box = False
print("To guess which box enter in the numbers that each box relates to, eg, Box 1 will be the number 1! Ready? Set? Go!")
while guess_box != in_box:
print(f"I was in box {in_box}")
guess_box = input("Which box? ")
if guess_box == in_box:
print("Great job, you found me!")
break
else:
print("I'm still hiding!!")
print("Thank you for playing")
否则,永远不会满足打破循环的条件。