3

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?

5 Answers 5

3

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.

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

Comments

1

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.

Comments

1

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

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

Comments

1

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 :)

Comments

0

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().

1 Comment

input in Python 2 evaluates the input automatically, which is one reason why you should always use raw_input. In Python 3 input acts like Py2's raw_input.

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.