Advertisement
GeorgiLukanov87

Guess the number game

Oct 17th, 2022 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. class bcolors:
  5.     HEADER = '\033[95m'
  6.     OKBLUE = '\033[94m'
  7.     OKCYAN = '\033[96m'
  8.     OKGREEN = '\033[92m'
  9.     WARNING = '\033[93m'
  10.     FAIL = '\033[91m'
  11.     ENDC = '\033[0m'
  12.     BOLD = '\033[1m'
  13.     UNDERLINE = '\033[4m'
  14.  
  15.  
  16. # Simple -> Guess the number game:
  17. # Simple game , you have 3 shots to pick a number and try to guess what is the computer number.
  18. # Rules: 1 correct number -> You win ! or 3 wrong answers -> You lose !
  19.  
  20. print(bcolors.HEADER + 'Welcome to the -> "GUESS THE NUMBER" <-' + bcolors.BOLD)
  21. print(bcolors.OKBLUE + 'You need to pick a number between 1 and 9 , Are you ready ? Go !' + bcolors.BOLD)
  22. print()
  23. possible_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  24. tries = 3
  25. random.shuffle(possible_numbers)
  26. winning_number = possible_numbers[0]
  27.  
  28. while True:
  29.     try:
  30.         tries -= 1
  31.         print()
  32.         player_number = int(input(bcolors.OKGREEN + 'Pick a number: ' + bcolors.BOLD))
  33.         if player_number == winning_number:
  34.             print(bcolors.OKGREEN + 'Well Done ! You win ! :)')
  35.             break
  36.  
  37.         else:
  38.             print(bcolors.FAIL + 'Wrong Number ! ' + bcolors.BOLD)
  39.             print(f'{bcolors.FAIL}Try Again -> tries left : {bcolors.HEADER}{tries}{bcolors.BOLD}')
  40.  
  41.             if winning_number > player_number:
  42.                 print(bcolors.OKCYAN + 'JOKER - > Winning number is Bigger!' + bcolors.BOLD)
  43.             else:
  44.                 print(bcolors.OKCYAN + 'JOKER - > Winning number is Lower!' + bcolors.BOLD + bcolors.ENDC)
  45.             print()
  46.  
  47.         if tries == 0:
  48.             print('Sorry, You LOST !:(')
  49.             answer = input('Wanna try again ? -> y / n').lower()
  50.             wrong_answers = 0
  51.             while answer not in ['y', 'n']:
  52.                 print('You need to choose -> "y" or "n" !')
  53.                 answer = input('"y" or "n"')
  54.                 wrong_answers += 1
  55.                 if wrong_answers == 2:
  56.                     break
  57.  
  58.             if answer == 'y':
  59.                 tries = 3
  60.                 continue
  61.             else:
  62.                 print(bcolors.HEADER + 'Program ending...Good Bye!' + bcolors.ENDC)
  63.                 break
  64.  
  65.     except ValueError:
  66.         print(bcolors.FAIL + 'INVALID DATA? , you need to pick a ' + bcolors.HEADER + 'NUMBER!' + bcolors.BOLD)
  67.         tries += 1
  68.         continue
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement