I've worked with while loops and the such before, but this one simply won't reach the conditions to break. This game is about finding hidden things in boxes. I've put a little bit of code that won't be there in the actual game that helps me verify what box is being hidden. The range of boxes is from 1 to 5 inclusive and is random each time the game starts again. I've started with the guess-box as false as I needed something to fill the space and turned the in_box into a string just in case.
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")
CodePudding user response:
You are setting in_box to a string and not saving it. you need to do 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")
Without this, the condition to break the loop is never met.
CodePudding user response:
you need to cast the input to an integer type, the default type for input() is str.
the result is logic like '1' == 1 , which is false so the condition never passes.
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")
works, note int() around guess-box in if condition.