Answered Essay: Second Post for same Q&A – Need assistance on this assignment creating a Python script in version 3.6:

Second Post for same Q&A – Need assistance on this assignment creating a Python script in version 3.6:

“Make-Up” Assignment: Espresso Prices

Overview

This assignment has several goals:

using loops for input validation.

gaining more practice designing and coding with functions.

creating functions with return values.

using conditional logic (if statements).

Please make sure that your solution meets these educational goals.

Important note

This is the “make-up” assignment. This assignment is in addition to the regular assignments in the course. It is provided for people who want some “extra” points for the quarter. You will only receive credit for this assignment if your quarter grade would be less than a 3.0 without it. Also, this assignment will only raise your quarter grade to a 3.0, no higher.

The Specifications

Your brother-in-law, the barista, has heard that you’re learning to program. He has asked you to program his POS (point of sale) system to handle his new price list. Fortunately, the POS system he has uses Python as its scripting language. You gladly accept the job.

Drinks

This is a simple espresso stand. It only sells three drinks.

Americano

Espresso

Cappuccino

As you can see, he’s not local, otherwise he would have to have a Cafe Latte on the menu as well.

Sizes

The drinks come in two sizes.

Medium, 12 oz.

Large, 16 oz.

Prices

The pricing on Americano is the simplest. Mediums are $0.99 per cup; larges, $1.29 per cup.

The pricing on Espresso is a bit more complicated. $1.75 per cup for medium; $2.25 per cup for large. However, there is a volume discount. The mediums are 3 for $4.25; larges are 3 for $5.50.

The pricing on Cappuccino is similar. $2.29 per cup for medium; $2.59 per cup for large. For cappuccino, the volume discount is 10% off for 4 or more cups.

The User Interface

Placing an order requires three pieces of information.

The type of drink
The type of drink can be handled using a single character: A E C

The drink size
The size can be handled using a single character as well: M L

The quantity
The quantity is an integer.

Based on the order, the POS system shall echo the order and calculate the price. All of the drinks in the order will be the same size and kind. The POS system shall collect the information using three separate questions.

Minimal Version

For the minimal version of the assignment, the system needs to handle only a single order. Here is an example. (In the examples which follow, bold is used to show user input is and the underscore character ( _ ) is used to show that the system is waiting for input.)

Enter number of drinks: 5
Enter drink size (M L): M
Enter kind of drink (A E C): E

The order is 5 medium espressos. Total bill: $7.75

Note: This is 1*$4.25 + 2*$1.75.

The minimal assignment only needs to accept upper-case responses.

Unrecognized input

If the user inputs a non-numeric value for the number of drinks the program shall not crash. The system shall print out an error message and ask again.

Enter number of drinks: 5R
Error in number of drinks. Please try again.
Enter number of drinks: _

If the number of drinks is not a positive integer value, the system shall print out an error message and ask again.

Enter number of drinks: 5R
Error in number of drinks. Please try again.
Enter number of drinks: 5.3
Error in number of drinks. Please try again.
Enter number of drinks: _

If the user inputs an unrecognized value for the size or kind of drink, the system shall print out an error message and ask again.

Enter number of drinks: 5
Enter drink size (M L): N
Error in drink size. Please try again.
Enter drink size (M L): _

The system shall keep asking until acceptable input is given. When the order is complete, the system shall echo the order and print out the total cost.

Enter number of drinks: 5R
Error in number of drinks. Please try again.
Enter number of drinks: 5.3
Error in number of drinks. Please try again.
Enter number of drinks: 5
Enter drink size (M L): N
Error in drink size. Please try again.
Enter drink size (M L): K
Error in drink size. Please try again.
Enter drink size (M L): M
Enter kind of drink (A E C): C

The order is 5 medium cappuccinos. Total bill: $10.31

Note: This is 5*$2.29 – 10%, or $11.45 – $1.145. The print-out shall always show cents, that is, have exactly two places after the decimal point. (Just let the system round the way that it wants.)

Notes for the Minimal Version

The judicious use of functions will simplify writing the assignment. You should have functions to handle input, processing, and output.

For example, a function could handle getting the number of drinks from the user. This would include:

displaying the initial prompt for the number of drinks

getting the input from the user

validating the input (checking to make sure that it is numeric and positive)

displaying the error message and asking again when the input is invalid

returning the validated input value

Each of the three input questions can be handled in this way.

Similarly, handling the calculation of the price can be separated into three different functions, one for each kind of drink. You can have a single function to get the price. It should take the number, size, and kind of drink as three parameters. Looking forward (if you read the extra-credit option), it will be best if this function returns the calculated price. This single price function can call the drink-specific price calculation functions.

And finally, you can create a function that will format and print the output. This is echoing the order and giving the total price.

One of the primary benefits of making all these smaller, separate functions is that you can work on them independently of each other. So, for example, once you have the calculations for espresso drinks working, you don’t have to mess with that code (and possibly mess it up) when you go to work on prices for a different kind of drink.

Another benefit of breaking the problem and its solution into smaller chunks, it makes the problem and its solution (the resulting code) more easy to understand and more manageable. In fact, the main for the minimal assignment can just call a few functions. Here it is in pseudo code.

get the number of drinks
get the drink size
get the kind of drink
calculate the price
print out the summary

Standard Version

For the standard version of the assignment, the POS system will be modified to handle multiple orders. After completing one order, the system shall ask if there is another order. If the answer is yes, the system prompt again for the new order. The single character ‘y’ is sufficient. The system shall also keep a running total of the orders filled. After the last order, the total value of the orders for the system shall be displayed.


The order is 5 medium cappuccinos. Total bill: $10.31

Is there another order? Y

Enter the number of drinks: _

The blank response, just pressing the return key, shall also be interpreted as ‘yes’.


The order is 5 medium cappuccinos. Total bill: $10.31

Is there another order?

Enter the number of drinks: _

Any response to this question which is neither ‘y’ nor empty shall be interpreted as “no” and the system shall end.


The order is 5 medium cappuccinos. Total bill: $10.31

Is there another order? B

Total orders for the day: $156.28

Remember that the running total of the orders needs to be kept by the system. The grand total shall be printed with exactly two places after the decimal. (Note: depending on your implementation, the grand total could be off from what you might expect, based on adding the total bill figures for the individual orders. This is due to rounding errors. However, it should not be off by more cents than the number of orders. So, if there were 15 orders, you would expect the grand total to be within 15 cents of sum of the 15 total bill figures given during the run of the program.)

In the standard version of the assignment, the POS system shall accept lower-case input as equivalent to upper-case.

Notes for the Standard Version

The use of functions to get the input and calculate the output should make moving from the minimal version to the standard version much easier. In fact, the change should be limited to main, the driver function that just calls the other “worker” functions.

Challenge Version

For the challenge version of the assignment, the system shall handle simplified order entry. That is, the number of drinks, the size, and the kind of drink can all be entered on the same line. You can assume the order of the elements on the input line are number, size, and kind.

Enter line item: 5 M E
This line item: 5 medium espressos, $7.75

However, if any of the inputs are not recognized, entry for the individual piece(s) will be requested.

Enter line item: 5 N E
Error in drink size. Please try again.
Enter drink size (M L): M
This line item: 5 medium espressos, $7.75

The observant student has probably noticed that the POS system now talks about line items. This is the other enhancement for the challenge version: an order can now consist multiple line items. You can think of this as modifying the minimal assignment order into a “line item”. A simple yes/no question, like the standard assignment asking for if there is another order, can be used to check if there are more line items.

Enter line item: 5 M E
This line item: 5 medium espressos, $7.75

More line items? _

At the end of the order, the system shall print out a summary of all of the line items and the total price.

Enter line item: 5 M E
This line item: 5 medium espressos, $7.75

More line items? y

Enter line item: 5 M C
This line item: 5 medium cappuccinos, $10.31

More line items? n

The total order: $18.06
5 medium espressos, $7.75
5 medium cappuccinos, $10.31

Is there another order? _

Note

Like the grand total for the standard version, the total bill may vary from the sum of the multiple “line items” for the bill. As noted before, the resulting total bill value can be off by multiple cents, though not to exceed the number of line items. So, if the complete order has three line items, the total for the order can differ from the project value by up to 3 cents and still be considered passing.

Test Cases

The test cases for the system are pretty straight forward. There are two kinds of test cases: one set for dealing with invalid input, and another for checking the calculations. There are several exams of test cases given in these notes. For the test cases, because we are letting Python handle the rounding, you can consider the test case as passing if the value reported by the script is within a penny of the calculated value.

Expert Answer

 

Challenging Version Code:

types = {'A': 'americanos', 'C': 'cappuccinos', 'E': 'espressos'}
sizes = {'M': 'medium', 'L': 'large'}


def take_order():
    order_total = 0.0  # To calculate total for line items
    line_item = input('Enter line item: ')
    split_line = line_item.split(' ')  # Splitting line item to process data
    if split_line[0].isdigit():  # If no. of drinks is integer
        drinks = int(split_line[0])
    else:  # If not then ask for proper data until provided
        print('nError in no. of drinks. Please try again.')
        drinks = input('Enter no. of drinks: ')
        while not drinks.isdigit():  # Loops until the data is an integer
            print('nError in no. of drinks. Please try again.')
            drinks = input('Enter no. of drinks: ')
        drinks = int(drinks)
    if split_line[1].upper() in sizes:  # If drink size is valid
        size = split_line[1].upper()
    else:  # else ask for valid data
        print('nError in drink size. Please try again.')
        size = input('Enter drink size (M L): ')
        while (size.upper() not in sizes):
            print('nError in drink size. Please try again.')
            size = input('Enter drink size (M L): ')
        size = size.upper()
    if split_line[2].upper() in types:  # If drink type is valid
        type = split_line[2].upper()
    else:  # Else ask for proper data
        print('nError in drink type. Please try again.')
        type = input('Enter drink type (A C E): ')
        while type.upper() not in types:
            print('nError in drink type. Please try again.')
            type = input('Enter drink type (A C E): ')
        type = type.upper()
    order_total = {  # A dictonary that uses anonymous functions to calculate total
        'A': lambda order: 0.99 * drinks if size == 'M' else 1.29 * drinks,
        'C': lambda order: (2.29 * drinks * 0.9 if size == 'M' else 2.59 * drinks * 0.9) if drinks > 3 else (2.29 * drinks if size == 'M' else 2.59 * drinks),
        'E': lambda order: int(drinks / 3) * 4.25 + (drinks % 3) * 1.75 if size == 'M' else int(drinks / 3) * 5.50 + (drinks % 3) * 2.25
    }[type](drinks)  # drinks is the parameter for lambda functions & type is the key
    line_item = '%d %s %s, $%.2f' % (drinks, sizes[size], types[type], order_total)  # line item result
    print('This line item: ', line_item)
    return line_item, order_total


def main():
    choice = 'y'
    while choice in 'yY':  # For multiple orders
        orders = []  # To store line items in a order
        total = 0.0
        line_items = 'y'
        while line_items in 'yY':  # For multiple line items
            order, amount = take_order()  # Taking order
            orders.append(order)  # Storing order
            total += amount  # Adding total
            line_items = input('nMore line items? ')  # Asking for more line items
            if line_items == '':
                line_items = 'y'
        # Showing order summary
        print('nThe total order: $%.2f' % total)
        for order in orders:
            print('t', order)
        choice = input('nIs there another order? ')  #Asking for more order
        if choice == '':
            choice = 'y'
    return

if __name__ == '__main__':
    main()

Output:

Running with Python 3.6 in Linux terminal:

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?