I am looking to improve the Python Number Guessing Game program:
How is the overall structure of the code?
Have I used functions to the best of their ability?
Can the code be simplified? If yes, please feel free to show how.
replay_game()function especially seems un-pythonic and overkill.
# Guess My Number Game # MacOSX (Latest), Python 3.4.3, 27/08/2017 # Guess The Computers number between 1-100 from random import randint from time import sleep def gen_number(): random_number = randint(0, 101) return random_number def intro(): print("Welcome to, Guess My Number!") print("Simply guess the number between 1-100 that the Computer is 'thinking' of!") print() def ask_number(question, random_number): response = None while response != random_number: try: response = int(input(question)) if response > random_number: print("Lower... ") elif response < random_number: print("Higher... ") else: correct = response congrat_winner(correct, random_number) except ValueError: print("Invalid. Enter a number between 1-100 ") return response def human_guess(): print("Ok Human! Let's begin... ") sleep(1) random_number = gen_number() # Named the variable random_number, the same as the variable in gen_number(). Is this good practise? guess = ask_number("Guess the number: ", random_number) def congrat_winner(correct, random_number): if correct == random_number: print() print("Calculating results...") sleep(1) print() print("WELL DONE!") print("The answer was indeed,", str(correct) + "!") def replay_game(): replay_question = None while replay_question != 'y' or 'n': replay_question = input("Would you like to play again (y/n)? ").lower() if replay_question == 'y': print() print("Rebuilding game... ") main() elif replay_question == 'n': print("Goodbye!") exit() else: print("please enter either 'y' or 'n'... ") def main(): intro() human_guess() replay_game() main()