Advertisement
kingbode

Untitled

Dec 8th, 2023 (edited)
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1.  
  2. import random
  3.  
  4.  
  5. def play_again():
  6.     ask_user = input("Do you want to play again ? (y/n) ")  # ask the user if he wants to play again
  7.     if ask_user == "y":
  8.         return True
  9.     return False
  10.  
  11. total_guesses = right_guesses = 0
  12. winner = False
  13.  
  14. right = random.randint(1,100)
  15.  
  16. while True:
  17.     if winner:
  18.         play_again()
  19.  
  20.     guess = (input("choose number between 1 and 100 : ")) # input
  21.  
  22.     # checking if the input is valid
  23.     if not guess.isdigit(): # if the input is not a number
  24.         print("enter a valid number ")
  25.         break
  26.  
  27.     if guess.isdigit(): # if the input is a number , dont break the loop and continue
  28.         guess = int(guess) # convert the input to integer (number)
  29.         total_guesses += 1 # add 1 to guesses
  30.  
  31.         if guess == right: # if the guess is correct
  32.             right_guesses += 1
  33.             print("\nYou win !!!!")
  34.             print(f"You made total of {total_guesses} guesses, having {right_guesses } correct guesses")
  35.             winner = True
  36.             continue
  37.  
  38.         elif guess > right : # if the guess is bigger than the correct number , dont break the loop and continue
  39.             print(f"{guess} is bigger than correct number ")
  40.  
  41.         elif guess < right : # if the guess is smaller than the correct number , dont break the loop and continue
  42.             print(f"{guess} is smaller than correct number ")
  43.  
  44.     else: # if the input is not a number
  45.         print("enter a valid number ")
  46.         play_again()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement