Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import random
  3. import math
  4.  
  5. g_low_boundary = 1
  6. g_high_boundary = 10
  7. g_initial_attempts = 0
  8. g_initial_num_of_hints = 5
  9.  
  10. def main():
  11.     global g_num_of_hints
  12.     global g_attempts
  13.  
  14.     while True:
  15.         g_num_of_hints = g_initial_num_of_hints
  16.         g_attempts = g_initial_attempts
  17.         main_loop()
  18.         decision = input(
  19.             'Type "n" if you don\'t want to play again or any symbol to continue: '
  20.         )
  21.         if decision.lower() == 'n':
  22.             print('Bye!')
  23.             break
  24.  
  25. def main_loop():
  26.     global g_num_of_hints
  27.     global g_attempts
  28.  
  29.     print('I am thinking about a number between %d and %d try to guess it!' % (
  30.         g_low_boundary,
  31.         g_high_boundary
  32.     ))
  33.  
  34.     num_to_guess = guess_a_number()
  35.  
  36.     while g_num_of_hints:
  37.         g_num_of_hints -= 1
  38.         g_attempts += 1
  39.  
  40.         guess = get_guess_input()
  41.  
  42.         if g_num_of_hints == 0:
  43.             give_a_final_try(guess, num_to_guess)
  44.         else:
  45.             success = give_a_hint(guess, num_to_guess)
  46.             if success:
  47.                 break
  48.  
  49. def guess_a_number():
  50.     number_list = list(range(g_low_boundary, g_high_boundary + 1))
  51.     return get_random_list_member(number_list)
  52.  
  53. def get_random_list_member(number_list):
  54.     ix = math.floor(random.random() * len(number_list))
  55.     return number_list[ix]
  56.  
  57. def get_guess_input():
  58.     guess = input('Put your guess here: ')
  59.  
  60.     while True:
  61.         if not guess:
  62.             guess = input('You do not have to pass empty value: ')
  63.             continue
  64.  
  65.         if not guess.isdigit():
  66.             guess = input('Your guest must be a number: ')
  67.  
  68.         break
  69.  
  70.     return int(guess)
  71.  
  72. def give_a_hint(guess, num_to_guess):
  73.     if guess < num_to_guess:
  74.         print('Please guess higher')
  75.     elif guess == num_to_guess:
  76.         on_success()
  77.         return True
  78.     else:
  79.         print('Please guess lower')
  80.     return False
  81.  
  82. def give_a_final_try(guess, num_to_guess):
  83.     if guess == num_to_guess:
  84.         on_success()
  85.     else:
  86.         on_fail()
  87.  
  88. def on_success():
  89.     print('Well done you guessed it with %d attempts and %d hints left!' % (
  90.         g_attempts,
  91.         g_num_of_hints
  92.     ))
  93.  
  94. def on_fail():
  95.     print('Sorry, you have no guessed correctly')
  96.  
  97. if __name__ == '__main__':
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement