In: Computer Science
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!
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: