Question

In: Computer Science

Shopping Bag: Assume the user has a bunch of purchases they would like to make. They...

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.

  1. 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: "))
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.)

Solutions

Expert Solution

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:


Related Solutions

used in python3 please add comment , thank you Shopping Bag: Assume the user has a...
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...
Often we would like to make the user think we like him/her so we use a...
Often we would like to make the user think we like him/her so we use a friendly prompt to get them to decide if they want to continue a loop. One can make a sentinel using an integer value outside of the expected input range but a more delightful way is to ask politely if: System.out.println("Do you want to play again?); In order to do this we will need a scanner and a do-while loop. For the scanner sometimes the...
I would like to prepare a survey regarding online purchases vs offline purchases. I would like...
I would like to prepare a survey regarding online purchases vs offline purchases. I would like to determine how customer satisfaction and customer’s value and perceptions are directly related in regards to online purchases vs offline purchases. I need 5-10 survey questions to ask.
You would like to make a buffer that has a pH of 7.00. Starting with 100.0...
You would like to make a buffer that has a pH of 7.00. Starting with 100.0 mL solution of 0.10 M NaH2PO4 and 0.10 M Na2HPO4, how much 0.1 M NaOH would you need to add to bring the pH of the buffer up to 7.00?
A company has just received a huge donation. It would like to make payments for its...
A company has just received a huge donation. It would like to make payments for its insurance for three years in advance. Previously, it has paid $100 in premiums every week. Assuming interest rate of 10%, what single sum of money should it pay?    (Note: the insurance payments are due at the beginning of the period of coverage, and compounding weekly). This is annuity due. a. 12,316.987 b. 10,051.421 c. 13,492.249 d. 15,118.687 e. 14,381.746
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
You are considering acquiring a common share of Grand Shopping Center Corporation that you would like...
You are considering acquiring a common share of Grand Shopping Center Corporation that you would like to hold for 1 year. You expect to receive both $1.25 in dividends and $41 from the sale of the share at the end of the year. If you wanted to earn a 13% return, the maximum price you would pay for a share today is: Question options: $42.05 $31.25 $38.41 $37.39
would like you to imagine that you have a black bag full of 20 marbles. All...
would like you to imagine that you have a black bag full of 20 marbles. All the marbles contained in the bag are the same size, and texture. The marbles in the bag are 5 red, 5 blue, 5 yellow and 5 green. What is the probability of pulling out 2 blue marbles from the bag assuming the first one is not replaced? What is the probability of pulling out 2 blue marbles from the bag assuming that the first...
1. Give an example of a product you would like to make and sell (like coffee...
1. Give an example of a product you would like to make and sell (like coffee - just pretend). a.What are some of the variable costs, fixed costs, mixed costs? b. What are some of the product costs versus period costs? c. What are the direct materials, direct labor, manufacturing overhead costs? Write this out as if you were giving it to a potential business partner
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT