Struggling with While Statements and Loops


#1

Hey everyone,

First a big thank you for everyone's help thus far as I've been working on learning the fundamentals of Python and an additional thank you for helping when my questions are often related to projects I am doing outside of the CA exercises.

So I am trying to create a program that asks the user to guess a number that will allow them to continue guessing until they guess the correct answer. However I am really stuck on getting the program to execute properly. I either am unable to get it to loop or I end up creating an infinite loop. If anyone could give me a hint or steer me in the right direction as to where I am messing this code up that would be greatly appreciated.

Thanks!

from random import randint
randomNum = randint(1,100)

name = input("What is your name? ")
print("Nice to meet you " + name + ".")
print()

guess = input("Please guess a number between 1 and 100: ")

while True:
    if (int(guess) > int(randomNum)):
      print("Im sorry " + name + " your guess is too high.")
    elif (int(guess) < int(randomNum)):
      print("Im sorry " + name + " your guess is too low.")
    elif guess == int(randomNum):
     break
    print("Yes " + name + " that is correct!")
print()

#2

First off, one can see a lot of int() calls, some of which are unnecessary.

randint()

Integer is right in the name. It returns one.

guess = int(input(" ... "))

if guess > randomNum:

elif guess < randomNum:

else:
    print ("Yes " + name + " that is correct!")
    break

#3

Hi mtf, thank you for your feedback!
I thought you needed to call int() to convert the string to an integer, no? I converted my code to your suggestion here and it kicks back the error message:
if guess > randomNum:
TypeError: unorderable types: str() > int()

When I added the int() call back to guess, to convert guess into an integer to compare with randomNum it began to work again. However it is not allowing multiple guesses. It just says your guess is too high/low and ends the process there. I thought having the While True at the start created the infinite loop and the break was put in place after the "correct" or else line to end that loop.. My head is kinda spinning on this one, sorry.. Pretty confused here.


#4

Did you pass the input() to int()? D;oh. Just read your second paragraph. Give me a minute, please.

Okay, we've dealt with the first issue. Now on to the second,

That's because the input should be inside the loop, not before it. You want them to be able to guess again.


#5

Ah, thank you! So I moved the guess input inside the loop and also removed the break command, which evidently was stopping anything from looping at all, as opposed to what I had thought which was it stopping only if the command it was under was met (the else line).

Problem solved, thank you again!


#6

Leave the break or you can never get out of the loop.

    else:
        print ("Yes " + name + " that is correct!")
        break

#7

Ah, the indentation I had my break set at was wrong. I had it aligned with else, not print.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.