0

I don't get the correct answer, it goes into an infinite loop, it doesn't recognize when I enter the number 7, it goes into the loop anyway

guessNum=input("please, give your guess number: ")
secretNum=7

while not guessNum == secretNum:
    print("Ha ha! You're stuck in my loop!")
    print("wrong number: ",guessNum)
    guessNum=input("please, give your guess number: ")

print("Well done, muggle! You are free now.")
jps
  • 20,041
  • 15
  • 75
  • 79
osk
  • 21
  • 1
  • 7
  • 1
    you are comparing a string against a number (add `int(input(...))` to cast string to int) – depperm Dec 20 '21 at 14:26
  • `input` always returns a string, so `guessNum` is a string, not a number – ForceBru Dec 20 '21 at 14:26
  • 1
    Does this answer your question? [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – depperm Dec 20 '21 at 14:28

2 Answers2

1

Aside from the type mismatch of comparing a str value from input to an int value, you should prefer a != b over not a == b, and structure the loop so that you only need one assignment to guessNum:

secretNum = 7

while True:
    guessNum = int(input("please, give your guess number: "))
    if guessNum == secretNum:
        break

    print("Ha ha! You're stuck in my loop!")
    print("wrong number: ", guessNum)

print("Well done, muggle! You are free now.")

or

secretNum = 7
while (guessNum := int(input("please, give your guess number: "))) != secretNum:
    print("Ha ha! You're stuck in my loop!")
    print("wrong number: ", guessNum)

print("Well done, muggle! You are free now.")
chepner
  • 497,756
  • 71
  • 530
  • 681
0
guessNum=int(input("please, give your guess number: "))
secretNum=7

while not guessNum == secretNum:
    print("Ha ha! You're stuck in my loop!")
    print("wrong number: ",guessNum)
    guessNum=int(input("please, give your guess number: "))

print("Well done, muggle! You are free now.")

add the int before input

Farshad
  • 38
  • 1
  • 8