From ef78a547ad5c25b534adc3cf22c3718d111a49ef Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Tue, 11 Apr 2017 16:16:33 -0700 Subject: [PATCH] Added python; stepwise example --- python/stepwise_number_game/main.py | 84 +++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 python/stepwise_number_game/main.py diff --git a/python/stepwise_number_game/main.py b/python/stepwise_number_game/main.py new file mode 100644 index 0000000..1b0824a --- /dev/null +++ b/python/stepwise_number_game/main.py @@ -0,0 +1,84 @@ +''' +Create a guessing game with a random number. +Go through it all using stepwise refinement before touch a char of code +make as many functions as reasonable + +Start: Use python along with importing the random lib +1. Print a welcome screen which also explains "q" to exit +2. Generate a random number + a. Create a function named random_number() + 1. Import random lib in order to generate a random number + 2. superSecreteNum = rand(1,100) +3. Get a guess from the user + a. create a function called ask_player_for_guess() + 1. playerGuess = input("Please enter a number 1-100: ") +4. Check to see if the user guessed correctly + a. Create a function called check_guess() + 1. If playerGuess.lower() is != "q" then + if int(playerGuess) == superSecreteNum then print "You WIN!!!" + else if print if greater than or less than the number + else return 0 +5. Ask if user would like to play again + a. create a function called play_again() + +end: Have a complete guessing (1-100) game that accepts user input for guesses +''' + +import random + +def random_number(): + rand_number = random.randint(1,100) + return rand_number + +def ask_player_for_guess(): + guess = input("Please enter a number (1-100): ") + return guess + +def check_guess(guess, answer): + if guess.lower != "q": + guess = int(guess) + if guess == answer: + print("Congrats! You guessed the right number!") + return True + + elif guess > answer: + print("You're a bit high yo!") + return False + + elif guess < answer: + print("Man, you might want to consider some anti-depressants") + return False + return 0 + +def play_again(): + choice = input("Would you like to play again? (y/n) ") + return choice + +def quit_game(): + print("Thanks for playing!") + return 0 + + +def main(): + print("Welcome to superSikrit, type \"q\" at any time to quit!") + superSecreteNum = random_number() + while True: + print("Answer is:", superSecreteNum) + playerGuess = ask_player_for_guess() + if playerGuess.lower() == "q": + quit_game() + break + result = check_guess(playerGuess, superSecreteNum) + #if the player wins... + if result == True: + result = False + choice = play_again() + print ("This was your choice:", choice) + if choice.lower() == "y": + superSecreteNum = random_number() + else: + quit_game() + break +main() + +