I want to repeat asking number process while guess is not equal with truenum.but when i put it in while loop it just repeats the print section of its "if" what should I do?
I want to repeat asking number process while guess is not equal with truenum in python.but when i put it in while loop it just repeats the print section of its "if" what should I do?
import random
# Defining True Num:
DataRange = list(range(0, 11))
TrueNum = int(random.choice(DataRange))
print("it`s in range from o to 10")
# Getting Guesses From User:
Guess = int(input("Input your number: "))
# Checking Guess:3
while Guess != TrueNum:
if Guess > TrueNum:
print("You Are Getting Far...")
elif Guess < TrueNum:
print("It`s bigger than you think :)")
break
if Guess == TrueNum:
print("yeah that`s it")
CodePudding user response:
You need to put the input inside the while, and is good also to take out some unnecessary conditions:
import random
# Defining True Num:
DataRange = list(range(0, 11))
TrueNum = int(random.choice(DataRange))
print("it`s in range from o to 10")
# Getting Guesses From User:
Guess = int(input("Input your number: "))
# Checking Guess:3
while Guess != TrueNum:
if Guess > TrueNum:
print("You Are Getting Far...")
else:
print("It`s bigger than you think :)")
Guess = int(input("Input your number: "))
print("yeah that`s it")
CodePudding user response:
The variable Guess is defined outside de loop, you should put the input and check the value inside the loop.
import random
datarange = list(range(0, 11))
trueNum = int(random.choice(datarange))
print("Guess the number! It`s in range from o to 10")
while True:
guess = int(input("Input your number: "))
if guess == trueNum:
print("Yeah that`s it")
break
elif guess > trueNum:
print("You Are Getting Far...")
else:
print("It`s bigger than you think :)")
CodePudding user response:
Using Try/Except allows you to address an invalid choice while also keeping readability.
import random
# Defining True Num:
DataRange = list(range(0, 11))
TrueNum = int(random.choice(DataRange))
print("it`s in range from o to 10")
# Getting Guesses From User:
while True:
try:
Guess = int(input("Input your number: "))
if Guess > TrueNum:
print("You Are Getting Far...")
elif Guess < TrueNum:
print("It`s bigger than you think :)")
elif Guess == TrueNum:
print("yeah that`s it")
break
except ValueError:
print("Invalid Choice")