Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import random
- import math
- g_low_boundary = 1
- g_high_boundary = 10
- g_initial_attempts = 0
- g_initial_num_of_hints = 5
- def main():
- global g_num_of_hints
- global g_attempts
- while True:
- g_num_of_hints = g_initial_num_of_hints
- g_attempts = g_initial_attempts
- main_loop()
- decision = input(
- 'Type "n" if you don\'t want to play again or any symbol to continue: '
- )
- if decision.lower() == 'n':
- print('Bye!')
- break
- def main_loop():
- global g_num_of_hints
- global g_attempts
- print('I am thinking about a number between %d and %d try to guess it!' % (
- g_low_boundary,
- g_high_boundary
- ))
- num_to_guess = guess_a_number()
- while g_num_of_hints:
- g_num_of_hints -= 1
- g_attempts += 1
- guess = get_guess_input()
- if g_num_of_hints == 0:
- give_a_final_try(guess, num_to_guess)
- else:
- success = give_a_hint(guess, num_to_guess)
- if success:
- break
- def guess_a_number():
- number_list = list(range(g_low_boundary, g_high_boundary + 1))
- return get_random_list_member(number_list)
- def get_random_list_member(number_list):
- ix = math.floor(random.random() * len(number_list))
- return number_list[ix]
- def get_guess_input():
- guess = input('Put your guess here: ')
- while True:
- if not guess:
- guess = input('You do not have to pass empty value: ')
- continue
- if not guess.isdigit():
- guess = input('Your guest must be a number: ')
- break
- return int(guess)
- def give_a_hint(guess, num_to_guess):
- if guess < num_to_guess:
- print('Please guess higher')
- elif guess == num_to_guess:
- on_success()
- return True
- else:
- print('Please guess lower')
- return False
- def give_a_final_try(guess, num_to_guess):
- if guess == num_to_guess:
- on_success()
- else:
- on_fail()
- def on_success():
- print('Well done you guessed it with %d attempts and %d hints left!' % (
- g_attempts,
- g_num_of_hints
- ))
- def on_fail():
- print('Sorry, you have no guessed correctly')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement