Reputation: 31
import random
play_game=input("Do you want to play ? Y/N :")
play_game=play_game.upper()
print(play_game)
while play_game == 'Y':
#Type of game does the user wish to play
game_type=input("please select a type:'H' for hard,'M' for medium ,'E' for easy :")
game_type=game_type.upper()
#print(game_type)
if game_type=='E':
#generating a random number between 1 and 10
random_number=random.randint(1,10)
print(random_number)
guess_times=0
#guessing a number
while guess_times<3:
user_number=(input("Enter the number \n:"))
user_number=int(user_number)
guess_times+=1
if user_number!=abs(user_number):
print("the number should be a +ve number")
else:
if user_number==random_number:
print("Congratulaions,%d is the correct guess and you guessed in %d attempts"%(user_number,guess_times))
break
if user_number<random_number:
print("the number,%d is lower than the guessed number"%user_number)
if user_number>random_number:
print("the number,%d is higher than the guessed number"%user_number)
else:
print("not the valid option")
print("bye")
I created a simple guess game above. It is working fine. But I want the user to play the game until the user decides to not to play.
I am using the while loop at the beginning of the game and once the user enters the game the option to quit is not being opted.
I do not want to use functions yet, as I am new to programming.
Upvotes: 0
Views: 471
Reputation: 652
You can add an option for game_type, .e.g Q for quit:
game_type=input("please select a type:'H' for hard,'M' for medium ,'E' for easy , Q to quit :")
And change the end of the code like this:
elif game_type == 'Q':
print("bye")
break
else:
print("not the valid option")
Upvotes: 0
Reputation: 1124828
You never update play_game
in your loop; you'll need to ask the user again at the end of the loop:
while play_game == 'Y':
# your game
play_game = input("Do you want to play again? Y/N :").upper()
Then, by the time the loop goes back to the top, if the user did not enter y
or Y
at that prompt, the game ends.
Upvotes: 1