I've created a number game where it asks the user if they want to play again and then the loop continues. My program uses import random but I want to know how I'd generate new random numbers without having to make variables each time. (I'm a beginner and I don't know what solution to use pls help)
My code works for the most part it's just that when the loop restarts the same number from the last playthrough repeats so the player ends up getting the same results. Here's my code:
`
import random
random_number_one = random.randint (0, 100)
username = input("Greetings, what is your name? ")
start_game = input("Welcome to the number game, {0}! Would you like to play a game? (Type 'Yes/No') ".format(username))
while True:
if start_game == 'Yes' or start_game == 'yes' :
print("Let's begin!")
print(random_number_one)
user_guess = input("Do you think the next number will be higher or lower? Type 'H' for Higher and 'L' for Lower: ")
if user_guess == 'H' or user_guess == 'h' :
print("You guessed higher. Let's see: ")
import random
random_number_two = random.randint (0, 100)
print(random_number_two)
if random_number_two > random_number_one :
print("You are correct! It was higher!")
play_again_h = input("Would you like to play again? ('Yes'/'No') ")
if play_again_h == 'Yes' or play_again_h == 'yes' :
continue
else:
break
else:
play_again = input("You were wrong, it was lower. Would you like to play again? ('Yes'/'No') ")
if play_again == 'Yes' or play_again == 'yes' :
continue
else:
break
elif user_guess == 'L' or user_guess == 'l':
print("You guessed lower. Let's see: ")
print(random_number_two)
if random_number_two < random_number_one :
print("You are correct! It was lower!")
play_again_l = input("Would you like to play again? ('Yes'/'No') ")
if play_again_l == 'Yes' or play_again_l == 'yes' :
continue
else:
break
else:
play_again_2 = input("You were wrong, it was higher. Would you like to play again? ('Yes'/'No') ")
if play_again_2 == 'Yes' or play_again_2 == 'yes' :
continue
else:
break
else:
print("Invalid response. You Lose.")
break
elif start_game == 'No' or start_game == 'no':
print("Okay, maybe next time.")
break
else:
print("Invalid response. You Lose.")
break
`
Aucun commentaire:
Enregistrer un commentaire