Advertisement
acclivity

pyGuessTheNumber

Nov 24th, 2021 (edited)
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. # A Number Guessing Game
  2. # Demonstration script
  3.  
  4. import random
  5.  
  6. max_tries = 4
  7. while True:
  8.     secret = random.randint(1, 20)
  9.     print("I am thinking of a number between 1 and 20. Can you guess it?\n")
  10.     for attempt in range(max_tries):
  11.         while True:
  12.             try:
  13.                 n = int(input("Enter your guess: "))
  14.                 break
  15.             except:
  16.                 print("You did not enter a number. Try again\n")
  17.         if n == secret:
  18.             print("Congratulations. You guessed correctly!")
  19.             break
  20.         elif n < secret:
  21.             print("Your guess is too low. ", end="")
  22.         else:
  23.             print("Your guess is too high. ", end="")
  24.         if attempt < (max_tries - 1):
  25.             print("Try again")
  26.         print()
  27.     else:
  28.         print("Bad luck! You ran out of tries.")
  29.     response = input("Play again? (N / Y): ")
  30.     if response[0] not in "Yy":
  31.         break
  32. print("Thanks for playing!")
  33.  
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement