Well, I so I have been trying to make this ‘guessing game’ in python where the user has to guess a number based on the clues.
But unfortunately, it doesn’t work the way I want it to.
I want it to go like-
1.Get single digit string from user, covert it to an integer
2.Making sure that the number isn’t the random number to be guessed, but a loop keeps giving the user clues based on the input
3. If the user enters the correct number, the game ends
Please help!!
What is the purpose of the num variable?
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.