Created
July 22, 2021 06:49
-
-
Save Mathsmaniac/45907f02bc6664128e812692c5b81a16 to your computer and use it in GitHub Desktop.
Includes post usability testing changes:
Adds introduction at start
Makes feedback messages clearer regarding wins and losses
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 v2 | |
Includes post usability testing changes: | |
Adds introduction at start | |
Makes feedback messages clearer regarding wins and losses | |
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(question.format(low, high))) | |
if low <= response <= high: | |
return response | |
else: | |
print(error) | |
print() | |
except ValueError: | |
print(error) | |
# Main routine goes here | |
# Introduction and instructions | |
print("*** Welcome to the Lucky Unicorn Game ***") | |
print() | |
print("To play, enter an amount of money between $1 a" | |
"nd $10 (whole dollars only).") | |
print("\nEach round costs $1") | |
print("\nPayouts:") | |
print("\tUnicorn: $5.00") | |
print("\tHorse or Zebra: $0.50") | |
print("\tDonkey: $0.00") | |
print() | |
# 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' + <enter> 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