gakonst

GuessTheNumber

Dec 10th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import random
  3. import sys
  4.  
  5. ### Strings
  6. PICK_NUMBER     = '[-->] Pick a number between 1 and 100\n> '
  7. NUMBER_BIGGER   = '[+] The hidden number is bigger'
  8. NUMBER_SMALLER  = '[-] The hidden number is smaller'
  9. WON_AFTER_TRIES = '[!] You found it after {} tries and won {} points'
  10. PLAY_AGAIN_PROMPT  = '[!] Do you want to play again? [y/n]\n> '
  11. INVALID_CHOICE  = '[-] Invalid choice. Try again'
  12. ERROR           = '[-] Nope! Current tries: {}'
  13. EXIT            = 'Exiting...'
  14. MENU = '''
  15. Welcome to 'Guess the Number'. The goal of the game is to find the random number the computer generates.
  16. Press:
  17.    1. Play
  18.    2. Exit'''
  19.  
  20. def computer_number():
  21.     return random.randint(1,100)
  22.  
  23. def number_input():
  24.     choice = 0
  25.     while not (1 <= choice <= 100):
  26.         try:
  27.             choice = int(input(PICK_NUMBER))
  28.             if (1 <= choice <= 100):
  29.                 break
  30.         except ValueError:    
  31.             print (INVALID_CHOICE)
  32.             continue
  33.         print(INVALID_CHOICE)
  34.     return choice
  35.  
  36. def play_again():
  37.     choice = input(PLAY_AGAIN_PROMPT)
  38.     while choice.lower() not in ['y', 'n']:
  39.         print(INVALID_CHOICE)
  40.         choice = input(PLAY_AGAIN_PROMPT)
  41.     if choice.lower() == 'n':
  42.         print(EXIT)
  43.         sys.exit()
  44.  
  45. def play_exit():
  46.     choice = (input('> '))
  47.     while choice not in ['1','2']:
  48.         print(INVALID_CHOICE)
  49.         choice = (input('> '))
  50.     if int(choice) == 2:
  51.         print (EXIT)
  52.         sys.exit()
  53.  
  54. def main():
  55.     # Loop infinitely, close only on ctrl+c or if user exits with predetermined commands
  56.     while True:
  57.         tries = 0
  58.         print(MENU)
  59.         # User input must be 1 to play, 2 to exit. All others are rejected
  60.        
  61.         play_exit()
  62.  
  63.         target = computer_number()
  64.         # print ('Target is', target) # for debugging purposes
  65.  
  66.         choice = number_input()
  67.  
  68.         while (choice is not target):
  69.             if choice > target:
  70.                 print (NUMBER_SMALLER)
  71.             elif choice < target:
  72.                 print (NUMBER_BIGGER)
  73.             tries += 1 # increment failed tries
  74.             print (ERROR.format(tries))
  75.             choice = number_input()
  76.            
  77.         print (WON_AFTER_TRIES.format( tries+1, 10 - (tries+1) ) )
  78.  
  79.         # print play again prompt, validate input
  80.         play_again()
  81.  
  82. if __name__ == '__main__':
  83.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment