print("Guess a number between 1 and 100")
userGuess = int(input("Your guess: "))
import random
randomNumber = (random.randint(1,100))
found = False
while userGuess in range(1,100):
if userGuess > randomNumber:
int(input("Guess lower: "))
found = False
elif userGuess < randomNumber:
int(input("Guess higher: "))
found = False
else:
print("You got it!")
found = True
When guessing the number, after your first guess it will use the right higher/lower if/elif statement, but every guess thereafter will be looped back into whichever statement was used the first time. Ends up repeating the same thing even if i go out of range. I know theres a lot of these threads for this program but i couldn't figure out what my issue was, any help is much appreciated!
Your while loop has no body. Indent the compound if statement to make it the body of the loop, ie
while userGuess in range(1,100):
if userGuess > randomNumber:
int(input("Guess lower: "))
found = False
elif userGuess < randomNumber:
int(input("Guess higher: "))
found = False
else:
print("You got it!")
found = True
Once you have that, you'll need to fix your logic. Most of all, you need to accept a new UserGuess after each wrong one. Your loop statement should likely be
while not found:
The statement you have destroys the user's input value.
you didn't assign input to userGuess in loop
import random
print("Guess a number between 1 and 100")
randomNumber = random.randint(1,100)
userGuess = int(raw_input("Your guess: "))
print randomNumber
found = False
while 1:
if userGuess < 1 or userGuess >100:
userGuess = int(input("input between 1 and 100 "))
continue
if userGuess == randomNumber:
print("You got it!")
break
userGuess = int(input("Guess lower: ")) if userGuess < randomNumber else int(input("Guess higher: "))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.