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")
2
  • 2
    str(in_box). In place? Commented Oct 18, 2021 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? Commented Oct 18, 2021 at 22:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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.
Actually, the str(in_box) would be unecessary on this example (you're not saving the generated string).
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.