In: Computer Science
This program will focus on utilizing functions, parameters, Python's math and random modules, and if-statements!
To Start: You should fork the provided REPL, run the program and observe the output, and then take a look through the code. You may not 100% understand the code, but taking a quick look through what's there will be helpful in the future.
You are going to systematically replace the TODO comments with code so that the program will work as intended.
Here are a couple sample runs of the program:
You're buying 5 drinks! Each drink will cost $3.75. ****** CUSTOMER DISCOUNT SUMMARY ****** --------------------------------------- Total purchase amount: $18.75 4.33% Discount: -$0.81 --------------------------------------- TOTAL AFTER DISCOUNT $17.94 --------------------------------------- You Saved $0.81 ---------------------------------------
main.py:
# TODO 0: Import the functions from coffee.py into your main so that the errors go away in this file.
# DO NOT move the functions over into main.py. You should use imports so that the functions can stay in coffee.py
# Save the randomly generated number of drinks into the total_drinks variable
total_drinks = get_num_drinks()
# Save the randomly generated drink price into the total_drinks variable
cost_per_drink = get_drink_price()
# The purchase amount before discounts is the number of drinks * their price
overall_price = total_drinks * cost_per_drink
# Use purchase amount to calculate sale discount, save into variable
purchase_discount = bulk_discount(total_drinks, cost_per_drink)
# Pass the purchase amount and the discount to the print_summary function
print_receipt(overall_price, purchase_discount)
coffee.py:
# TODO 0: Import math and random, or just the functions you need from those modules
# get_num_drinks
# Parameters: none
# Return: should generate and return the number of drinks to order
# Side effect: should print the number of drinks
def get_num_drinks():
num_drinks = 0 # TODO 1: generate a random number between 2 and 12
print() # TODO 2: print how many drinks were ordered to the screen, in a full sentence.
return num_drinks
# get_drink_price
# Parameters: none
# Return: should choose and return one of four drink costs
# Side effect: should print the drink price
def get_drink_price():
drink_type = 0 # TODO 3: generate a random number representing the drink type
drink_price = 0
# TODO 4: write if-statements based on drink_type that will set drink_price to the appropriate value
print() # TODO 5: print the drink price to the screen, in a full sentence including $ and punctuation.
return drink_price
# bulk_discount
# Parameters: none
# Return: A discount equal to the square root of the
# total purchase amount, in decimal form.
# e.g. If the customer spend $25, this is a 5% discount, and should be returned as .05.
# Side effect: if the user buys at least six drinks, AND each drink costs at least $4, print "Wow, you must really like your friends!"
def bulk_discount(total_drinks, price_per_drink):
amount = 0.0 # TODO 6: calculate the square root of the purchased amount, using the math module. Remember to return the percentage as a decimal, e.g. .05 rather than 5.
# TODO 7: if the user buys at least six drinks, and each drink costs at least $4, print "Wow, your friends must really like you!"
return amount
# print_receipt
# Parameters: amount spent, discount
# Return: none
# Side effect: prints a summary of the customer's purchase and discount savings
def print_receipt(original_total, discount):
human_readable_discount = 0 # TODO 8: Generate the human-readable discount from the discount variable, e.g. .06 should be 6, to print a 6% discount
saved = original_total * discount
new_total = original_total - discount
# TODO 9: Fix the formatting of the summary below to print exactly 2 decimal places for each number
# TODO 10: Check the output and make sure that it makes sense. If not, it means there are bugs still in the code.
print("****** CUSTOMER DISCOUNT SUMMARY ******")
print("---------------------------------------")
print("Total purchase amount: \t\t $", original_total, sep="")
print(human_readable_discount, "% Discount: \t\t\t -$", saved, sep="")
print("---------------------------------------")
print("TOTAL AFTER DISCOUNT\t\t $", new_total, sep="")
print("---------------------------------------")
print("You Saved\t\t\t\t\t $", discount, sep="")
print("---------------------------------------")
I have completed the code and also attached the output
I have not done any changes in the previously written TODO comments and you can use them to easily understand what the code is doing.
I have also fixed some bugs from the provided code, so now it will work fine.
main.py
# TODO 0: Import the functions from coffee.py into your main so that the errors go away in this file.
from coffee import get_num_drinks, get_drink_price, bulk_discount, print_receipt
# DO NOT move the functions over into main.py. You should use imports so that the functions can stay in coffee.py
# Save the randomly generated number of drinks into the total_drinks variable
total_drinks = get_num_drinks()
# Save the randomly generated drink price into the total_drinks variable
cost_per_drink = get_drink_price()
# The purchase amount before discounts is the number of drinks * their price
overall_price = total_drinks * cost_per_drink
# Use purchase amount to calculate sale discount, save into variable
purchase_discount = bulk_discount(total_drinks, cost_per_drink)
# Pass the purchase amount and the discount to the print_summary function
print_receipt(overall_price, purchase_discount)
coffee.py
# TODO 0: Import math and random, or just the functions you need from those modules
import math
import random
# get_num_drinks
# Parameters: none
# Return: should generate and return the number of drinks to order
# Side effect: should print the number of drinks
def get_num_drinks():
num_drinks = random.randint(2,12) # TODO 1: generate a random number between 2 and 12
print("Number of Drinks ordered: ",num_drinks) # TODO 2: print how many drinks were ordered to the screen, in a full sentence.
return num_drinks
# get_drink_price
# Parameters: none
# Return: should choose and return one of four drink costs
# Side effect: should print the drink price
def get_drink_price():
types = [0, 1, 2, 3]
drink_type = random.choice(types) # TODO 3: generate a random number representing the drink type
prices = [3.00, 3.75, 4.00, 4.50]
drink_price = 0
# TODO 4: write if-statements based on drink_type that will set drink_price to the appropriate value
if drink_type == 0:
drink_price = prices[0] # $3.00
if drink_type == 1:
drink_price = prices[1] # $3.75
if drink_type == 2:
drink_price = prices[2] # $4.00
if drink_type == 3:
drink_price = prices[3] # $4.50
print("The Price of the drink is: $", drink_price) # TODO 5: print the drink price to the screen, in a full sentence including $ and punctuation.
return drink_price
# bulk_discount
# Parameters: none
# Return: A discount equal to the square root of the
# total purchase amount, in decimal form.
# e.g. If the customer spend $25, this is a 5% discount, and should be returned as .05.
# Side effect: if the user buys at least six drinks, AND each drink costs at least $4, print "Wow, you must really like your friends!"
def bulk_discount(total_drinks, price_per_drink):
amount = total_drinks * price_per_drink # TODO 6: calculate the square root of the purchased amount, using the math module. Remember to return the percentage as a decimal, e.g. .05 rather than 5.
discount = math.sqrt(amount)/amount
# TODO 7: if the user buys at least six drinks, and each drink costs at least $4, print "Wow, your friends must really like you!"
if total_drinks>=6 and price_per_drink>=4.00:
print("Wow, your friends must really like you!")
return float(discount)
# print_receipt
# Parameters: amount spent, discount
# Return: none
# Side effect: prints a summary of the customer's purchase and discount savings
def print_receipt(original_total, discount):
human_readable_discount = discount * 100 # TODO 8: Generate the human-readable discount from the discount variable, e.g. .06 should be 6, to print a 6% discount
saved = original_total * discount
new_total = original_total - saved
# TODO 9: Fix the formatting of the summary below to print exactly 2 decimal places for each number
# TODO 10: Check the output and make sure that it makes sense. If not, it means there are bugs still in the code.
print("****** CUSTOMER DISCOUNT SUMMARY ******")
print("---------------------------------------")
print("Total purchase amount: \t\t ${:.2f}".format(original_total, sep=""))
print("{:.2f}% Discount: \t\t -${:.2f}".format(human_readable_discount, saved, sep=""))
print("---------------------------------------")
print("TOTAL AFTER DISCOUNT\t\t ${:.2f}".format(new_total, sep=""))
print("---------------------------------------")
print("You Saved\t\t\t ${:.2f}".format(saved, sep="") )
print("---------------------------------------")
Output
If you like the answer,
please upvote