Question

In: Computer Science

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 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.

Solutions

Expert Solution

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:


Related Solutions

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...
C++ OOP Provide a user-friendly way to enter expressions to be evaluated. Please add a comment...
C++ OOP Provide a user-friendly way to enter expressions to be evaluated. Please add a comment explaining each step of your program. Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token...
use python3 language and need answer ASAP please thank you. The primary sources of calories in...
use python3 language and need answer ASAP please thank you. The primary sources of calories in food are fat, carbohydrate, and protein. Proteins and carbohydrates have four calories per gram, and fat has nine calories per gram. For example, on a box of Honey Nut Cheerios, it says that a serving has 1.5 grams of fat, 22 grams of carbohydrates, and 2 grams of protein. That works out to: 1.5 * 9 + 22 * 4 + 2 * 4...
PLEASE answer using EXCEL. Add formulas and steps used. Thank you! 5. A company is 42%...
PLEASE answer using EXCEL. Add formulas and steps used. Thank you! 5. A company is 42% financed by risk-free debt. The interest rate is 12%, the expected market risk premium is 10%, and the beta of the company’s common stock is 0.52. What is the after-tax WACC, assuming that the company pays tax at a 40% rate.
Python3  Please add comments Write a program that implements the word guessing game - There is a...
Python3  Please add comments Write a program that implements the word guessing game - There is a secret word - The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess. If the user reveals all the letters in the word before getting too many wrong guesses then they win! Otherwise, they lose. 1 - define secret word 2 - create a revealed letter list that...
Please answer ASAP, thank you! 1. Please present, analyze, and comment on a (specific or general)...
Please answer ASAP, thank you! 1. Please present, analyze, and comment on a (specific or general) monetary or fiscal policy enacted by the Federal Reserve Bank or by the Federal Government to help our economy by describing the particular context in which that policy was enacted.
In Python3 please do the following: Program MUST take USER INPUT. The examples below are just...
In Python3 please do the following: Program MUST take USER INPUT. The examples below are just test cases for you to test the program. You are given a rare document that we suspect is written in an alien language. The alien language has the following grammar. All its words read the same backward as well as forward. E.g. aba. A sequence of these words are written as sentences. It is composed of only English alphanumeric([a-z, A-Z, 0-9]) characters in it....
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT ALREADY BEEN POSTED, please use code from the assignment Copy-paste will be reported Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers...
Please write a basic function using Python. Please comment all steps. Thank you! Experimentally determined molecular...
Please write a basic function using Python. Please comment all steps. Thank you! Experimentally determined molecular structures are stored in the Protein Data Bank. Protein Data Bank format is a standard for files containing atomic coordinates which are stored in the “ATOM” record. Write a Python function to extract x coordinate of each atom for a given PDB file. Test your function with the provided “1a3d.pdb” file as the example. Also, give a good thought what would be the proper...
NOTE: Please provide instructions on how to solve it in excel add screenshots Thank you Suppose...
NOTE: Please provide instructions on how to solve it in excel add screenshots Thank you Suppose a random sample of 137 households in Chicago was taken as part of a study on annual household spending for food at home. The sample data is below. For the sample data, compute the mean and the median and construct a box and whisker plot. (Use Excel to Descriptive statistics and copy paste the screenshot here). Are the data skewed or symmetric? Approximately what...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT