Skip to content

Instantly share code, notes, and snippets.

@Mathsmaniac
Created July 6, 2021 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mathsmaniac/2dbd86a3fb735801b9c6767956f42aab to your computer and use it in GitHub Desktop.
Save Mathsmaniac/2dbd86a3fb735801b9c6767956f42aab to your computer and use it in GitHub Desktop.
Includes constants for payout amounts and loop range
"""
Component 2 of Lucky Unicorn - v2 advanced testing
Include constants for payout amounts and loop range
Format currency for payout calculation
Created by Nathan Smith
28/06/21
"""
import random
HOW_MUCH = 100 # Constant representing the amount spent by user
UNICORN_PAYOUT = 5
HORSE_ZEBRA_PAYOUT = 0.5
# Using counters to keep track of the frequency of each token
donkey_count = 0
horse_zebra_count = 0
unicorn_count = 0
# Balancing the odds - now only a 1/10 chance of getting a unicorn
options = ['donkey', 'donkey', 'donkey', 'zebra', 'zebra', 'zebra',
'horse', 'horse', 'horse', 'unicorn']
# Generating 100 random tokens
for i in range(HOW_MUCH): # Using the constant rather than a literal
option_chosen = random.choice(options)
print(option_chosen)
if option_chosen == 'donkey':
donkey_count += 1
elif option_chosen == 'zebra' or option_chosen == 'horse':
horse_zebra_count += 1
else:
unicorn_count += 1
# Money calculations
winnings = unicorn_count * UNICORN_PAYOUT + horse_zebra_count *\
HORSE_ZEBRA_PAYOUT # Line break \ above to keep under 79 chars
# Summary of results
print("\nDonkeys: {}".format(donkey_count))
print("Horses and Zebras: {}".format(horse_zebra_count))
print("Unicorns: {}".format(unicorn_count))
# Summary of spending and winnings
print("\nYour spend ${:.2f}".format(HOW_MUCH)) # formatted to 2dp
print("You go home with ${:.2f}".format(winnings))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment