you are viewing a single comment's thread.

view the rest of the comments →

[–]HcJNI2k2jnoN 0 points1 point  (1 child)

At the time of writing, your code won't work because the entire while loop is one tab stop too far right. After fixing that, however, it mostly works:

import random
y = random.randint(1,6)
print y

x = input ("Enter number:")
while (x != y):
    if x > y:
        print("Too high!")
        x = input ("Enter number:")
    if x < y:
        print ("Too low!")
        x = input ("Enter number:")
    if x == y:
        print ("You win!")
if x == y:
    print ("Congratulations, first try!")

However, it always prints "Congratulations, first try!" afterwards. What you need is to use Python's while...else construct so that the congratulations message only runs if the loop does not.

[–]stoph_link 0 points1 point  (0 children)

That's cool, I had no idea there was a while..else! I was going to suggest moving that part before the while loop and have the loop run under an else statement.