Created
July 20, 2021 21:10
-
-
Save Mathsmaniac/4fb89277f44fbe7b83a311f79d05a76b to your computer and use it in GitHub Desktop.
First version of fully working program, not yet tested for usability
This file contains 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
"""Lucky Unicorn - fully working program, combining all 4 components | |
First version of fully working program, not yet tested for usability | |
Created by Nathan Smith | |
28/06/21 | |
""" | |
import random | |
# Integer checking function | |
def intcheck(question, low, high): | |
valid = False | |
while not valid: | |
error = "Whoops! Please enter an integer between {} " \ | |
"and {}".format(low, high) | |
try: | |
response = int(input("Enter an integer between {} " | |
"and {}: ".format(low, high))) | |
if low <= response <= high: | |
return response | |
else: | |
print(error) | |
print() | |
except ValueError: | |
print(error) | |
# Main routine goes here | |
# Constant values | |
UNICORN_PAYOUT = 5 | |
HORSE_ZEBRA_PAYOUT = 0.5 | |
GAME_FEE = 1 | |
MAX_AMOUNT = 10 | |
# Ask how much the user wants to play with (minimum $1, maximum $10) | |
user_balance = intcheck("How much money would you like to play" | |
" with? ", GAME_FEE, MAX_AMOUNT) | |
keep_playing = "" | |
while not keep_playing: | |
# Balancing the odds - now only a 1/10 chance of getting a unicorn | |
options = ['donkey', 'donkey', 'donkey', 'zebra', 'zebra', 'zebra', | |
'horse', 'horse', 'horse', 'unicorn'] | |
# Generate random choice of token from list | |
option = random.choice(options) | |
print("\nYou got a {}".format(option)) | |
# Adjust user_balance correctly for given token | |
if option == "unicorn": | |
user_balance += 4 | |
print("Congratulations, you won ${:.2f}".format(UNICORN_PAYOUT)) | |
elif option == "horse" or option == "zebra": | |
user_balance -= 0.5 | |
print("Congratulations, you won ${:.2f}".format(HORSE_ZEBRA_PAYOUT)) | |
else: | |
user_balance -= 1 | |
print("Sorry, you did not win anything this round") | |
# Check and report user_balance, giving option to play again if at least $1 | |
if user_balance >= GAME_FEE: | |
print("You have ${:.2f} left to play with\n".format(user_balance)) | |
keep_playing = input("Press <enter> to play another round o" | |
"r 'q' to quit: ") | |
else: | |
print("and do not have enough credit for another round.") | |
keep_playing = "q" | |
# Farewells user at the end of the game | |
print("\nYou are leaving with a balance of ${:.2f}\nThanks for play" | |
"ing, Goodbye.".format(user_balance)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment