Last active
August 29, 2015 14:06
-
-
Save xander-miller/5eca69d2b57504d30614 to your computer and use it in GitHub Desktop.
I know this is very basic, but I'm super proud of my first python program in 5 years.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| def show_guesses(): | |
| guesses.append(guess) | |
| print(str(guesses) + " {} more out of 5.".format(5 - len(guesses))) | |
| def show_help(): | |
| print("Guess a number between '1' and '10'") | |
| print("'DONE' quits and 'HELP' displays this help.") | |
| def too_big(): | |
| print("{} is too big".format(guess)) | |
| def too_small(): | |
| print("{} is too small".format(guess)) | |
| def reset_game(): | |
| global guesses | |
| global magic_num | |
| guesses = [] | |
| magic_num = random.randint(1,10) | |
| print("Okay, ready! I've picked a number.") | |
| guesses = [] | |
| magic_num = 0 | |
| reset_game() | |
| show_help() | |
| while True: | |
| if len(guesses) >= 5: | |
| print("Sorry, you lose. The number was {}.".format(magic_num)) | |
| reset_game() | |
| response = input("> ") | |
| try: | |
| guess = int(response) | |
| except: | |
| if response == 'DONE': | |
| break | |
| elif response == 'HELP': | |
| show_help() | |
| continue | |
| if guess == magic_num: | |
| print("That's it! The number was {}. You win! Play again? (Y/n)".format(guess)) | |
| play_again = input('> ') | |
| if play_again == 'n': | |
| break | |
| else: | |
| reset_game() | |
| elif int(guess) > magic_num: | |
| too_big() | |
| show_guesses() | |
| elif int(guess) < magic_num: | |
| too_small() | |
| show_guesses() | |
| continue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| def show_help(): | |
| print("Guess a number between '1' and '10'") | |
| print("'DONE' quits and 'HELP' displays this help.") | |
| def pick_a_num(): | |
| return random.randint(1,10) | |
| def too_big(): | |
| print("Too big") | |
| def too_small(): | |
| print("Too small") | |
| magic_num = pick_a_num() | |
| show_help() | |
| while True: | |
| response = input("> ") | |
| try: | |
| guess = int(response) | |
| if guess == magic_num: | |
| print("That's it! You win! Play again? (Y/n)") | |
| play_again = input('> ') | |
| if play_again == 'n': | |
| break | |
| else: | |
| magic_num = pick_a_num() | |
| elif int(guess) > magic_num: | |
| too_big() | |
| elif int(guess) < magic_num: | |
| too_small() | |
| except: | |
| if response == 'DONE': | |
| break | |
| elif response == 'HELP': | |
| show_help() | |
| continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment