anatta
anatta

Reputation: 107

python while loop unexpected behavior

I'm relatively new to Python, and I don't understand the following code produces the subsequently unexpected output:

x = input("6 divided by 2 is")
while x != 3:
    print("Incorrect. Please try again.")
    x = input("6 divided by 2 is")
    print(x)

the output of which is:

6 divided by 2 is 3
Incorrect. Please try again.
6 divided by 2 is 3
3
Incorrect. Please try again.
6 divided by 2 is 

Why is the while loop still being executed even though x is equal to 3?

Upvotes: 3

Views: 284

Answers (5)

BradTheBrutalitist
BradTheBrutalitist

Reputation: 299

Here is my answer to your question

Guesses = 0
while(Guesses < 101):
    try:
        x = int(input("6 divided by 2 is: "))
        if(x == 3):
            print("Correct! 6 divide by 2 is", x)
            break
        else:
            print("Incorrect. try again")
            Guesses += 1
    except ValueError:
        print("That is not a number. Try again.")
        Guesses += 1
else:
    print("Out of guesses.")

I am assuming you wanted the user to input a number so i put your code into a while\else loop containing a try\except loop. The try\except loop ensures that if the users inputs a number, a ValueError will appear and it will inform them that what they inputted was not a number. The while\else loop ensures that the user will be inputted the question if the Guesses limit is no higher than 100. This code will ensure that if the user guesses the answer which is 3, the user will be prompted that they got the answer right and the loop will end; if the users guesses anything besides 3 (Number wise) the answer will be incorrect and the user will be prompted the question again; if the user guesses a string it will be classified as a ValueError and they will be notified that their answer wasn't a number and that the user has to try again.

Considering this was asked a long time ago, I'm assuming your probably forgot about this or you figured out the answer but if not try this and tell me if you like this answer. Thank :)

Upvotes: 1

Homer
Homer

Reputation: 219

I actually tried this myself now with python 2.6, and did get an int without converting to int. For example, when I try the following:

x = input("6 divided by 2 is")
print "Your input is %s, %s" % (x, type(x))

I get the following:

6 divided by 2 is 2
Your input is 2, <type 'int'>

So is this a version issue? Or maybe an environment issue (I'm using OS X)? What I do conclude is that it should be a good practice using previous recommendations using int().

Upvotes: 0

Emile P.
Emile P.

Reputation: 3962

input() returns a string, which you are comparing to an integer. This will always return false. You'll have to wrap input() in a call to int() for a valid comparison.

x = int(input("6 divided by 2 is"))
while x != 3:
    print("Incorrect. Please try again.")
    x = int(input("6 divided by 2 is"))
    print(x)

Read more on int() here.

Upvotes: 3

Saurav Gharti Magar
Saurav Gharti Magar

Reputation: 174

input method gives the string. So you need to typecast to int as:

x = int(input("6 divided by 2 is"))

Upvotes: 1

user3520669
user3520669

Reputation:

You are getting this error is because you are not parsing the input like so:

x = int(input("6 divided by 2 is"))

If you replace your inputer statement with that, it'll work.

Upvotes: 1

Related Questions