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.
1. Write a Python program that will ask the user length and width of the right...
1. Write a Python program that will ask the user length and width of the right triangle and find the area of the right-angled triangle. The formula for finding the area of a right-angle triangle is ab/2. Also, find out the result if you calculate as (ab)/2. Is it the same? If it is same, why it is the same. If it is not the same, why it is not the same.
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.
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
1. Write a Python program to: ask the user to enter two integers: int1 and int2....
1. Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT