a. Display this to the console:
Bananas: $0.50 each
Strawberries: $.75 each
b. Ask the user what they want to purchase. They will type an 'b' for bananas or an 's' for strawberries.
c. Your program will then ask for the number of bananas or strawberries they wish to purchase (depending on which they selected). The number of bananas cannot exceed 20 and the number of strawberries cannot exceed 10. If the user input exceeds these maximums your program should exit.
d. Your program should then print a report to the console like the following:
Item Qty Price Total
--------------------------
Bananas 4 $0.50 $2.00
Tax(15%): $0.30
Total Cost: $2.30
e. Ask the user for the amount tendered (the amount of money given for the payment). If the amount tendered is over $100 tell the user that there isn't enough change for such an amount and exit.
f. Finally, your program should print another report to the console like the following:
Amount tendered: $10
Change: $7.70
Thank you for shopping with SAI!
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
#Display the commodities available for purchase
print("\nWelcome to SAI shooping center")
print("\nb.Bananas: $0.50 each")
print("s.Strawberries: $0.75 each")
#prompt the user to enter what she/he wants to buy
option = input("Enter the of what you want to purchase:")
if option == "b":
number_of_bananas = int(input("Enter the number of bananas to purchase: "))
if number_of_bananas > 20:
print("You can not purchase more than 20 bananas, please try again")
else:
#calculate total price
total = 0.5 * number_of_bananas;
print("\nItem Qty Price Total")
print("Bananas "+str(number_of_bananas)+" $0.50 $"+str(total))
#calculate tax 15%
tax = 0.15*total;
#print tax
print("Tax(15%): $"+str(tax))
#calculate total cost
total_cost = total + tax
#print the total cost
print("Total cost: $"+str(total_cost))
#prompt the user to enter the amount of money tendered
money_tendered = int(input("Enter the amount tendered: "))
if money_tendered > 100:
print("Dear customer there isn't enough change for such an amount.")
else:
print("Amount tendered: $"+str(money_tendered))
#calculate change
change = money_tendered - total_cost
print("Change: $"+str(change))
print("Thank you for shopping with SAI!")
elif option == "s":
number_of_strawberries = int(input("Enter the number of Strawberries to purchase: "))
if number_of_strawberries > 10:
print("You can not purchase more than 10 Strawberries, please try again")
else:
#calculate total price
total = 0.75 * number_of_strawberries;
print("\nItem Qty Price Total")
print("Strawberries "+str(number_of_strawberries)+" $0.75 $"+str(total))
#calculate tax 15%
tax = 0.15*total;
#print tax
print("Tax(15%): $"+str(tax))
#calculate total cost
total_cost = total + tax
#print the total cost
print("Total cost: $"+str(total_cost))
#prompt the user to enter the amount of money tendered
money_tendered = int(input("Enter the amount tendered: "))
if money_tendered > 100:
print("Dear customer there isn't enough change for such an amount.")
else:
print("Amount tendered: $"+str(money_tendered))
#calculate change
change = money_tendered - total_cost
print("Change: $"+str(change))
print("Thank you for shopping with SAI!")
else:
print("Invalid option, please try again")
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment