Question

In: Computer Science

This program will focus on utilizing functions, parameters, Python's math and random modules, and if-statements! To...

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.

  1. TODO 0 is in both main.py and coffee.py. Import the necessary functions from coffee.py into main.py. Then in coffee.py either import all of math and random right away, or import just the functions that you need as you write the code. DO NOT move the functions over to main.py. Use imports so that those functions can stay in coffee.py.
  2. TODO 1 is inside the get_num_drinks() function. You should set the variable num_drinks to a random number from 2 and 12 (inclusive) to simulate the number of drinks the customer is ordering. This is not user input. This is a computer-generated pseudorandom number using Python's random module (Links to an external site.).
  3. TODO 2 is below TODO 1. Print the random amount spent to the screen (see the first line in the sample outputs.)
  4. TODO 3 is in the get_drink_price() function. Generate one of 4 random numbers to be used in TODO 4.
  5. TODO 4 is below TODO 3. You need to write a series of if statements that use the random number from TODO 3 to select one of 4 random drink prices: $3.00, $3.75, $4.00, $4.50.
  6. TODO 5 is below TODO 4. Print the random drink price to the screen (see the second line in the sample outputs.)
  7. TODO 6 is in the bulk_discount() function. You should calculate the bulk discount as the square root of the total price. The total price is equal to the number of drinks * price of each drink. You will need to use Python's math module to make this calculation. Note that this function should return a decimal number for the discount. 25% = 0.25 discount
  8. TODO 7 is below TODO 6. Conditionally print a message to the screen. If the user buys at least 6 drinks and each drink costs at least $4, print a message to the user about how nice they are to their friends.
  9. TODO 8 is in the print_summary() function. Since the parameter discount is in the format 0.25 to be used in calculations, we need a new variable that contains 25 in order to print the receipt in a human-readable way.
  10. TODO 9 is below TODO 7. Round this to only 2 decimal places show when printing a number to the screen. So instead of something like 23.45666666667 printing, we want 23.45 (or 23.46). Essentially, we want all references to money to print with 2 decimal places (though trailing zeros may be missing).
  11. TODO 10. Bugs?! What? My code has bugs? Indeed it does. If you check out the receipt, the total after discount and savings are not correct. In print_summary, correct where the calculations are being made so that the summary reads properly.

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("---------------------------------------")

Solutions

Expert Solution

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


Related Solutions

Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be...
Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be in basic python. Part 1. Write a program that does the following: 1. Create a list of length N where N is a randomly selected integer between 10 and 20 and whose elements are randomly selected integers between 0 and 19. 2. Print out the list. 3. Create a copy of the list. Sort the copy into decreasing order and print it. 4. Report...
create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
Discret Math MATH/CSCI2112 (3) (a) Write the following premises and the conclusion as logical statements and...
Discret Math MATH/CSCI2112 (3) (a) Write the following premises and the conclusion as logical statements and prove the conclusion correct: (use the symbols in brackets). If the flight is late, I will spend the night in Toronto. If I miss my flight I will spend the night in Winnipeg. Either my flight is not late, or I did not miss my flight from Winnipeg, but not both. Therefore either I will spend the night in Toronto, or I will spend...
Implement the following functions in a module called arrayfunctions.py. You must use the booksite modules stdarray...
Implement the following functions in a module called arrayfunctions.py. You must use the booksite modules stdarray and stdio to implement the functions.   print1Darray() takes a one dimensional integer array argument and prints it (field width for each element should be 5). print2Darray() takes a two dimensional integer array argument and prints it (field width for each element should be 5 - should be printed as a m x n matrix). add2Darrays() takes 2, two dimensional integer array arguments. You can...
Describe any three program modules common to an accounting system software package.
Describe any three program modules common to an accounting system software package.
Describe the anatomy and physiology of the Pancreas. Focus on the exocrine digestive functions.
Describe the anatomy and physiology of the Pancreas. Focus on the exocrine digestive functions.
In this lab assignment, students will demonstrate the abilities to: - Use functions in math module...
In this lab assignment, students will demonstrate the abilities to: - Use functions in math module - Generate random floating numbers - Select a random element from a sequence of elements - Select a random sample from a sequence of elements (Python Programming) NO BREAK STATEMENTS AND IF TRUE STATEMENTS PLEASE Help with the (create) of a program to play Blackjack. In this program, the user plays against the dealer. Please do the following. (a) Give the user two cards....
Using the concepts from the Concurrency Basics Tutorial I provided in Modules, write a program that...
Using the concepts from the Concurrency Basics Tutorial I provided in Modules, write a program that consists of two threads. The first is the main thread that every Java application has. The main thread should create a new thread from the Runnable object, MessageLoop, and wait for it to finish. If the MessageLoop thread takes too long to finish, the main thread should interrupt it. Use a variable named maxWaitTime to store the maximum number of seconds to wait. The...
1. Null and alternative hypotheses are statements about A. population parameters. B. sample parameters. C. sample...
1. Null and alternative hypotheses are statements about A. population parameters. B. sample parameters. C. sample statistics. D. population statistics. E. It depends on the situation and the type of data has been collected. 2. A hypothesis test is conducted under the initial assumption that A. the alternative hypothesis is false. B. the alternative hypothesis is true. C. the null hypothesis is false. D. the null hypothesis is true. E. We never make any assumptions when conducting a hypothesis test....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT