0

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")
Diroallu
  • 824
  • 1
  • 11
  • 15
Skyfall
  • 5
  • 4
  • 2
    `str(in_box)`. In place? – TheMaster Oct 18 '21 at 22:52
  • "but this one simply won't reach the conditions to break." In your own words, why *should* it ever? Can you give an example of something the user could `input`, that would match the `in_box` value? Hint: what is the *type* of `in_box`? What is the type of the `input` result? – Karl Knechtel Oct 18 '21 at 22:57

2 Answers2

2

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.

3ddavies
  • 546
  • 2
  • 19
0

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.

G.S
  • 535
  • 3
  • 8
  • Feels like `in_box = str(randrange(1, 5))` is closer to what OP was trying to do, as indicated by the `str(in_box)` expression. Also calling `str` on the value won't throw an error, while `int(guess_box)` will if the user enters non-numeric characters. – Henry Woody Oct 18 '21 at 22:55
  • Actually, the `str(in_box)` would be unecessary on this example (you're not saving the generated string). – Diego Ramirez Oct 18 '21 at 22:58
  • All true, tbh, I didn't read most of the code I just knew it would be a type mismatch so fixed that to make the example work. – G.S Oct 18 '21 at 22:59