In: Computer Science
used in python3 please add comment , thank you
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.
SOURCE CODE:
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")
choice = int(input("Make a choice: ")) #reading choice from user
if choice==1: #if choice is 1 then
price=int(input("Enter price: ")) #reading price and number of items
num_items=int(input("Enter number of items: "))
cost=get_cost(price,num_items) #calling get_cost() function
global shopping_bag #declaring shopping_bag as global variable
if(cost>0): #if cost is positive then adding it to shopping_bag if not error
shopping_bag=cost
else:
print("Invalid price or cost!")
main() #calling main()
elif choice==2: #if choice is 2 then printing total in shopping_bag
print("Total in the shopping_bag: ",shopping_bag)
main() #calling main()
elif choice==3: #if choice is 3 then resetting shopping_bag
shopping_bag=0;
main() #calling main()
elif choice==4: #if choice is 4 then good bye messaage
print("good bye!")
else: #else invalid choice message
print("Your choice is invalid!")
main() #calling main
def get_cost(price,num_items):
if(price>0 and num_items>0): #if price and num_items are positive then
return price*num_items
else: #else returning -1
return -1
main() #calling main()
SCREENSHOT FOR INDENTATION:
OUTPUT: