Aidan McCourt
Aidan McCourt

Reputation: 13

I don't know what to do with my Number Guess Code

Somewhat new to Python, I have everything else down except for everything after the while statement. Please help!

I don't know what to do after this

while y > x or y < x:
 if y == x:
   print ("You got it, " + Name + "!")
else:
if (y < x):
  print ("Higher!")
else:
  print ("Lower!")
  break


import random
print("Hey, what's your name?")
Name = input("")
print ("What do you want to be the maximum number?")
maximnum = int(input(""))
print("I just thought of a number between 1 and " + str(maximnum) + ", can you guess it " + Name + "?")
y = input("")
x = random.randint(1,maximnum)
print (x)
while y > x or y < x:
if y == x:
print ("You got it, " + Name + "!")
else:
if (y < x):
  print ("Higher!")
else:
  print ("Lower!")
  break

It just keeps running and doesn't stop

Upvotes: 1

Views: 42

Answers (1)

Andrew F
Andrew F

Reputation: 2950

The condition for your while loop is y>x or y<x which is equivalent to y!=x. If at any point y is equal to x, the loop will end. From the looks of your code sample, neither x nor y change within the loop, so if it enters with them being unequal, it will never exit. One solution would be to add an extra input line inside of the loop

while y != x:
    if (y < x):
        print ("Higher!")
    else:
      print ("Lower!")
    y = input('Guess again... ')
print ("You got it, " + Name + "!")

Upvotes: 3

Related Questions