Question

In: Computer Science

Calculating Delivery Cost Program in Python write a program in Python that will ask a user...

Calculating Delivery Cost Program in Python

write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost.

Purchase total > $150

Yes

Number of the items (N)

N<=5

N>=6

Delivery day

Same Day

Next Day

Same Day

Next Day

Delivery charges ($)

8

N * 1.50

N * 2.50

N * 1.20

Prompt the user to enter the purchase total. Pass the purchase total to a function as an argument to validate the purchase total above $150 and return True/False from the function to the calling code. If the return value is False, show an error message and asks the user if he/she wants to continue. If the return value from the first function is True, call another function that calculates the cost of delivery for a customer.

Ask a user to enter the number of the items that need to be delivered and delivery day and pass them to the function. Return the cost of delivery and the total cost to the calling code and display them to the user.

Make your program loop, prompting the user for whether they would like to calculate another delivery cost. Keep track of the number of the purchase and when the user exits, print out the total number of the purchase that entered to the system and the total cost of deliveries.

Your program should be able to handle some exceptions and invalid inputs, such as:

·Negative values.

·Invalid delivery day (1 for same day delivery and 2 for second-day delivery).

·Non-numeric and empty values

For example, if a user enters “$200” as purchase total, “7” as the number of the items and “1” as delivery in the same day, the program should be able to display the delivery cost of “17.50" and the total cost of “$217.50” ($200 + $17.50 ) for the user.

A typical example of the display of your program can be as follows. Your program MUST follow the same display style.

---------------------------------------------------------------------------------
Welcome to delivery charges Calculator
----------------------------------------

Please enter purchase total: 200
Please enter number of the items: 7

Please enter delivery day ([1] for 1st day and [2] for 2nd day): 1

Delivery charges: $17.50
Total cost: $217.50

Do you want to calculate delivery charges for another purchase? (y/n): y

Please enter purchase total: 90

ERR: Sorry, purchase total need to be above $150.
Do you want to calculate delivery charges for another purchase? (y/n): n

Thanks for using the delivery charges Calculator!

See you again!

Solutions

Expert Solution

Program Code to Copy:

def greaterthan150(purchase):
    # Returns true if purchase greater than 150
    if purchase > 150:
        return True
    return False


def calculatecost(n, day):
    # Code if n greater than 5
    if n <= 5:
        # Code if it is same day
        if day == 1:
            delivery_charges = 8
        # Code if it is next day
        else:
            delivery_charges = n * 1.50
    else:
        # Code if it is same day
        if day == 1:
            delivery_charges = n * 2.50
        # Code if it is next day
        else:
            delivery_charges = n * 1.20

    return delivery_charges


print('---------------------------------------------------------------------------------')
print('Welcome to delivery charges Calculator')
print('----------------------------------------')

choice = 'y'
while choice == 'y':
    try:
        purchase_cost = int(input('Please enter purchase total:'))
        if purchase_cost < 0:
            raise Exception
        elif not greaterthan150(purchase_cost):
            print('ERR: Sorry, purchase total need to be above $150.')
        else:
            n_items = int(input('Please enter number of the items:'))
            delivery_day = int(input('Please enter delivery day ([1] for 1st day and [2] for 2nd day):'))
            if delivery_day != 1 and delivery_day != 2:
                print('Delivery day invalid')
            else:
                delivery_charges = calculatecost(n_items, delivery_day)
                total = purchase_cost + delivery_charges
                print('Delivery charges: $%.2f' % delivery_charges)
                print('Total cost: $%.2f' % total)
    except:
        print('Entered value for number of items,delivery day,purchase cost is not an integer or negative')
    choice = input('Do you want to calculate delivery charges for another purchase? (y/n):')

print('Thanks for using the delivery charges Calculator!\nSee you again!')

Program Code Screenshot:

Program Output:


Related Solutions

In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
In your python program, ask the user to enter the annual income of an employee and...
In your python program, ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee’s years of experience is greater than 4, if so the employee gets $6000 loan; otherwise,...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Task 1. creating a nested if:   Need a python program that ask the user to enter...
Task 1. creating a nested if:   Need a python program that ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee's years of experience is greater than 4, if...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT