you can do this by using while True:
loop. and you don’t need num
variable.
import random and then generate a random number and store it in a variable as you did before i.e.
import random
guessNum=random.choice(range(1,100))
create a while True:
loop
inside the while True :
loop take input from user i.e. inp=input("Enter a number = ")
then use try
to convert input into integer if user input non numeric value then use except
and inside except
use continue
statement to continue the loop. inside try
first convert input into integer i.e. x=int(inp)
.next check if x>guessNum:
then print("Higher")
and continue the loop using continue
statement .next check elif x<guessNum:
then print("Lower")
and continue the loop using continue
statement.next use else:
statement. inside else
condition print('You won')
and break the while True:
loop using break
statement.