Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def play_again():
- ask_user = input("Do you want to play again ? (y/n) ") # ask the user if he wants to play again
- if ask_user == "y":
- return True
- return False
- total_guesses = right_guesses = 0
- winner = False
- right = random.randint(1,100)
- while True:
- if winner:
- play_again()
- guess = (input("choose number between 1 and 100 : ")) # input
- # checking if the input is valid
- if not guess.isdigit(): # if the input is not a number
- print("enter a valid number ")
- break
- if guess.isdigit(): # if the input is a number , dont break the loop and continue
- guess = int(guess) # convert the input to integer (number)
- total_guesses += 1 # add 1 to guesses
- if guess == right: # if the guess is correct
- right_guesses += 1
- print("\nYou win !!!!")
- print(f"You made total of {total_guesses} guesses, having {right_guesses } correct guesses")
- winner = True
- continue
- elif guess > right : # if the guess is bigger than the correct number , dont break the loop and continue
- print(f"{guess} is bigger than correct number ")
- elif guess < right : # if the guess is smaller than the correct number , dont break the loop and continue
- print(f"{guess} is smaller than correct number ")
- else: # if the input is not a number
- print("enter a valid number ")
- play_again()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement