Advertisement
Programmin-in-Python

Guess the Number Game

Dec 28th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. from random import randint
  2.  
  3. def guess():
  4.     num = randint(0,1000)
  5.     tries = 0
  6.     won, won_rew = False, False
  7.  
  8.     while True:
  9.         try:
  10.             usr_num = int(input("Guess the Number between 0 and 1000 : "))
  11.             tries += 1
  12.  
  13.             if usr_num == num:
  14.                 if tries <= 5:
  15.                     won_rew = True
  16.                     break
  17.                 else:
  18.                     won = True
  19.                     break
  20.             else:
  21.                 if usr_num < num:
  22.                     print("Actual Number is Larger...")
  23.                 else:
  24.                     print("Actual Number is Smaller...")
  25.  
  26.         except ValueError:
  27.             print("You've Entered a WRONG Value...\n")
  28.  
  29.     if won_rew:
  30.         print("You Won!!! It took you {} tries to guess the Actual Number...\nYou Get a Reward".format(tries))
  31.     elif won:
  32.         print("You Won!!! It took you {} tries to guess the Actual Number...".format(tries))
  33.  
  34. def main():
  35.     choice = 'y'
  36.  
  37.     while choice.lower() in ('y', 'n'):
  38.         guess()
  39.  
  40.         choice = input("Do You want to Play Again (y/[n]) : ")
  41.  
  42.         if choice.lower() == 'y':
  43.             continue
  44.         elif choice.lower() == 'n':
  45.             print("Thank You For Playing the Game...")
  46.             break
  47.         else:
  48.             print("Invalid Choice...")
  49.  
  50. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement