Created
July 22, 2021 08:38
-
-
Save Mathsmaniac/0b89cf44b2b482eabfb3a4dd15c50923 to your computer and use it in GitHub Desktop.
Added round counter and included round details in feedback to player
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 v3 | |
Added round counter and included round details in feedback to player | |
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) | |
# Function to format token statements | |
# Takes message and differentiating decoration to top and bottom | |
def token_statement(statement, char): | |
print() | |
print(char * len(statement)) | |
print(statement) | |
print(char * len(statement)) | |
print() | |
# 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 track of rounds played - to include in feedback | |
num_rounds = 0 | |
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) | |
num_rounds += 1 | |
# Adjust user_balance correctly for given token | |
if option == "unicorn": | |
user_balance += 4 | |
token_statement("****** Congratulations! It's a {}. You won" | |
" ${:.2f} ******" | |
"".format(option, HORSE_ZEBRA_PAYOUT), "*") | |
elif option == "horse" or option == "zebra": | |
user_balance -= 0.5 | |
token_statement("^^^^^^ Congratulations! It's a {}. You won" | |
" ${:.2f} ^^^^^^" | |
"".format(option, HORSE_ZEBRA_PAYOUT), "^") | |
else: | |
user_balance -= 1 | |
token_statement("------ Congratulations! It's a {}. You won" | |
" ${:.2f} ------" | |
"".format(option, HORSE_ZEBRA_PAYOUT), "-") | |
# Check and report user_balance, giving option to play again if at least $1 | |
if user_balance >= GAME_FEE: | |
print("Rounds played: {} You have ${:.2f} left to play" | |
" with\n".format(num_rounds, user_balance)) | |
keep_playing = input("Press <enter> to play another round o" | |
"r 'q' + <enter> to quit: ") | |
else: | |
print("Rounds played: {} but do not have enough credit for " | |
"another round.".format(num_rounds)) | |
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