There are I have two problem please help
problem 1 - input should be integer 1-100 rand_num = random.randint(1,100)
if input goes 100 above then show enter valid number 1 to 100 how could I achieve this please help
Thanks!
and also Could please give me a another suggestion to make my project more better.
import random
LIVES = 10
SCORE = 0
HIGH_SCORE = 0
print("\t\t =============> Welcome to the number guessing game developed by Python <===============\n")
print("You have only 10 lives to guessing the number\n\
")
rand_num = random.randint(1,100)
print(rand_num)
while LIVES >=0:
try:
user = int(input("Choose a number between 1-100 : "))
if user == rand_num:
print("Congratulations You guessed it right.")
SCORE=11-LIVES
if SCORE > SCORE:
SCORE = HIGH_SCORE
print(f"Your score is {SCORE} ")
print(f"The Score Is {HIGH_SCORE}")
break
elif user > rand_num:
LIVES-=1
print(f"Too high! Please guess lower number.\n Current Lives= {LIVES}")
elif user < rand_num:
LIVES-=1
print(f"Too Low! Please guess higher number.\n Current Lives= {LIVES}")
except Exception as e:
print(e)
CodePudding user response:
I added a few embellishments to your code so that it could be played more than once.
import random
LIVES = 10
SCORE = 0
HIGH_SCORE = 0
DONE = False
print("\t\t =============> Welcome to the number guessing game developed by Python <===============\n")
print("You have only 10 lives to guessing the number\n")
rand_num = int(random.randint(1,100))
print(rand_num)
while True:
while LIVES >=0:
try:
user_string = input("Choose a number between 1-100 : ")
if user_string == "Q" or user_string == "q":
DONE = True
break
user = int(user_string)
if user > 100 or user < 1: # To account for invalid guesses
print("Guesses must be from 1 to 100")
continue
if user == rand_num:
print("Congratulations You guessed it right.")
SCORE = LIVES
print(f"Your score is {SCORE} ")
if SCORE > HIGH_SCORE:
print("This is also a new high score!!!")
HIGH_SCORE = SCORE
print(f"The Current High Score Is {HIGH_SCORE}")
break
elif user > rand_num:
LIVES-=1
print(f"Too high! Please guess lower number.\n Current Lives= {LIVES}")
elif user < rand_num:
LIVES-=1
print(f"Too Low! Please guess higher number.\n Current Lives= {LIVES}")
except Exception as e:
print(e)
if (DONE == True):
break
# Let's do this again
print("\t\t =============> Welcome to the number guessing game developed by Python <===============\n")
print("You have only 10 lives to guessing the number\n")
rand_num = int(random.randint(1,100))
print(rand_num)
LIVES = 10
SCORE = 0
First off, I ensured that the user input was an integer by wrapping the value inside the "int" function. Next, per your request, I added a range test of the entered value so that it only allows guesses from "1" to "100". Then, I also added in another "while" loop to provide for a continuous number of games. That way, the number of tries could be kept and compared to any previous high score to see if a new high score was achieved. In order to exit the game and the highest level "while" loop a test is made to see if the user has entered "Q" or "q" to quit (you might want to add verbiage to the program to direct the user).
Anyway, try those out and see if that is close to what your are trying to accomplish.