In: Computer Science
Shopping Bag:
Assume the user has a bunch of purchases they would like to make. They enter the price of each item, and the quantity and they want to add it to their cart. They tell us if they have more items to purchase, and if they do, we repeat getting the data and add the cost to the shopping bag. When they're done, we display the total. We will not account for Tax (yet)
We will create a textual menu that shows 3 choices.
1. Enter item.
2. Show total cost.
3. Start Over.
4. Quit.
The user is expected to enter 1, and then enter a price and the number of units. They can then choose 1 and enter another item price and units, or choose 2, and the code displays their total cost. If the user enters 3, we need to reset the shopping cart. After the user chooses options 1,2 or 3, the code executes that menu option then shows the menu again. When the user chooses menu option 4, we do not repeat the menu.
This following is a partial code for main that you should add to, to make a complete application.
choice = int(input("Make a choice: "))
if #choice is 1
#get price and no. of items, pass them to get_cost
function
#then add the result to shopping_bag (which must be global)
#after checking the result is positive, otherwise display an error
message
#call main
elif #choice is 2
#display the total in the shopping_bag
#call main
elif #choice is 3
#reset the shopping_bag
#call main
elif #choice is 4
#display a good bye message
else
#display a message that choice is invalid
#call main
You need to write the function to check each item cost and
return that cost.
The function must check that the price and the number of items are
both positive numbers, otherwise it returns -1.
Call main at the end.
Once you finish writing the code make sure to test it properly
before uploading.
Add comments as you see fit but make sure to add top level
comments.
(for extra practice, add the tax to the total. Allow at least three cities tax rates.)
Code:
shopping_bag=0#gloabl variable shopping bag
def get_cost(price,quantity):
return price*quantity#returning the cost of the item
def main():
print("Enter 1 to enter a price then the quantity to add to your
shopping bag")
print("Enter 2 to to show your shopping bag cost")
print("Enter 3 to start a new shopping bag")
print("Enter 4 to end")#printing the Menu
while(True):#infinet while loop
global shopping_bag
choice = int(input("Make a choice: "))#reading users choice
if(choice==1):#if choice is 1
price=float(input("Enter the price of the item:"))
quantity=int(input("Enter no of items:"))
#reading price and quantity
result=get_cost(price,quantity)#getting the total price
if(result<0):#if result is negetive displaying the error
message
print("Error! Price and no of items should be only positive")
else:#else we add the total price to the shopping bag
shopping_bag+=result
elif(choice==2):#if choice is 2 we print the cost of shopping
bag
print("Cost of shopping bag:",shopping_bag)
elif(choice == 3):#if choice is 3 we make shoping bag as 0
global shoping_bag
shopping_bag=0
#prining the meanu again
print("Enter 1 to enter a price then the quantity to add to your
shopping bag")
print("Enter 2 to to show your shopping bag cost")
print("Enter 3 to start a new shopping bag")
print("Enter 4 to end")
elif (choice==4):#if choice is 4
#break the loop
print("Good Bye!")
break;
else:
#if wrong choice
print("Choice is invalid")
main()
Output:
Indentation: