Added python; stepwise example
This commit is contained in:
parent
53bf1e4edf
commit
ef78a547ad
84
python/stepwise_number_game/main.py
Normal file
84
python/stepwise_number_game/main.py
Normal file
@ -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()
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user