![]() |
beverage machine code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: beverage machine code (/thread-13071.html) |
beverage machine code - ccm1776 - Sep-26-2018 Hello, everyone. Im new to programming and Ireally love solving problems. I dont have all the answers, however. I am taking a python class at my local CC. We had a project about writing an interface for a beverage machine. I have already been graded on this, I turned it in incomplete because I got stuck on the final part. I am having trouble figuring out how to write the interface for accepting payment and comparing it to the balance of the sale. When I try to use an if statement after "userPayment=", I keep getting an EOF while parsing. If you could give me feedback on that, how to write the final part of my program which does userpayment>totalprice and prints the change to be dispensed or not enough credit for sale. or any other part of my code that would be great. I dont think its written very efficiently so if I have any lines that are not necessary and can be written in less lines I would also like to know that. This is actually the first program Ive ever written. Thanks in advance. #Create Welcome drinkPrice0=float(1.50) drinkPrice1=float(1.25) drinkPrice2=float(1.75) drinkPrice3=float(2.00) drinkMedium=int(2) drinkLarge=int(3) print('Hello and welcome to the beverage center') print(""" """) #listing items available to purchase print('Here are the choices available') print(""" """) print('Item 1: Coca-Cola...Small $1.50...Medium $3.00...Large $4.50') print('Item 2: Root Beer...Small $1.25...Medium $2.50...Large $3.75') print('Item 3: Bottle Water...Small: $1.75...Medium $3.50...Large $5.25') print('Item 4: Apple Juice...Small $2.00...Medium $4.00...Large $6.00') print(""" """) #user selecting drink while True: try: drinkChoice=int(input('Enter the number of your item please: ')) if drinkChoice==1: print(""" """) print('Coca-Cola selected...Small $1.50...Medium $3.00...Large $4.50') break elif drinkChoice==2: print(""" """) print('Root Beer selected...Small $1.25...Medium $2.50...Large $3.75') break elif drinkChoice==3: print(""" """) print('Bottle Water selected...Small $1.75...Medium $3.50...Large $5.25') break elif drinkChoice==4: print(""" """) print('Apple Juice selected...Small $2.00...Medium $4.00...Large $6.00') break except ValueError: print(""" """) print('Invalid item number, try again.') else: break #user selecting size print('Now enter the size of your drink') print(""" """) print("'S' for Small") print("'M' for Medium") print("'L' for Large") print(""" """) #drink price and size multiplied sizeSelected=input('What size would you like? ') while True: try: if drinkChoice==1 and sizeSelected.upper()=='S': smallCoke=float(1.50) print('Your total is $ ', format(smallCoke,'.2f')) break elif drinkChoice==1 and sizeSelected.upper()=='M': mediumCoke = float(drinkPrice0*drinkMedium) print('Your total is $ ', format(mediumCoke,'.2f')) break elif drinkChoice==1 and sizeSelected.upper()=='L': largeCoke=float(drinkPrice0*drinkLarge) print('Your total is $ ', format(largeCoke,'.2f')) break elif drinkChoice==2 and sizeSelected.upper()=='S': smallRootBeer=float(drinkPrice1) print('Your total is $ ', format(smallRootBeer,'.2f')) break elif drinkChoice==2 and sizeSelected.upper()=='M': medRootBeer=float(drinkPrice1*drinkMedium) print('Your total is $ ', format(medRootBeer,'.2f')) break elif drinkChoice==2 and sizeSelected.upper()=='L': largeRootBeer=float(drinkPrice1*drinkLarge) print('Your total is $ ',format(largeRootBeer,'.2f')) break elif drinkChoice==3 and sizeSelected.upper()=='S': smallWater=float(drinkPrice2) print('Your total is $ ',format(drinkPrice2,'.2f')) break elif drinkChoice==3 and sizeSelected.upper()=='M': medWater=float(drinkPrice2*drinkMedium) print('Your total is $ ',format(medWater,'.2f')) break elif drinkChoice==3 and sizeSelected.upper()=='L': largeWater=float(drinkPrice2*drinkLarge) print('Your total is $ ',format(largeWater,'.2f')) break elif drinkChoice==4 and sizeSelected.upper()=='S': appleJuice=float(drinkPrice3) print('Your total is $ ',format(drinkPrice3,'.2f') ) break elif drinkChoice==4 and sizeSelected.upper()=='M': medAppleJuice=float(drinkPrice3*drinkMedium) print('Your total is $ ',format(medAppleJuice,'.2f')) break elif drinkChoice==4 and sizeSelected.upper()=='L': largeAppleJuice=float(drinkPrice3*drinkLarge) print('Your total is $ '),format(largeAppleJuice,'.2f') break except ValueError: print ("One of your inputs was invalid, please try again") continue #establishing a final price for user to pay if drinkChoice==1 and sizeSelected.upper()=='S': totalPrice=float(drinkPrice0) elif drinkChoice==1 and sizeSelected.upper()=='M': totalPrice = float(drinkPrice0*drinkMedium) elif drinkChoice==1 and sizeSelected.upper()=='L': totalPrice=float(drinkPrice0*drinkLarge) elif drinkChoice==2 and sizeSelected.upper()=='S': totalPrice=float(drinkPrice1) elif drinkChoice==2 and sizeSelected.upper()=='M': totalPrice=float(drinkPrice1*drinkMedium) elif drinkChoice==2 and sizeSelected.upper()=='L': totalPrice=float(drinkPrice1*drinkLarge) elif drinkChoice==3 and sizeSelected.upper()=='S': totalPrice=float(drinkPrice2) elif drinkChoice==3 and sizeSelected.upper()=='M': totalPrice=float(drinkPrice2*drinkMedium) elif drinkChoice==3 and sizeSelected.upper()=='L': totalPrice=float(drinkPrice2*drinkLarge) elif drinkChoice==4 and sizeSelected.upper()=='S': totalPrice=float(drinkPrice3) elif drinkChoice==4 and sizeSelected.upper()=='M': totalPrice=float(drinkPrice3*drinkMedium) elif drinkChoice==4 and sizeSelected.upper()=='L': totalPrice=float(drinkPrice3*drinkLarge) userPayment=float(input('Enter amount of money to be inserted, including dollars and cents $ ')) RE: beverage machine code - ichabod801 - Sep-26-2018 You need to give us the if condition that is causing problems, and the full text of the error you are getting. As given, I ran the code and it was fine, so I don't know how to fix the problem with code I haven't seen. Have you done dictionaries yet? All those if then statements could be totally simplified by a dictionary. drink_prices = {(1, 'S'): 1.5, (1, 'M'): 3.0, (1, 'L'): 4.5, (2, 'S'): 1.25, ...} ... total_price = drink_prices[(drinkChoice, sizeSelected.upper())]That would eliminate almost all of your if/elif clauses. RE: beverage machine code - woooee - Sep-27-2018 And if you are not comfortable using dictionaries (get comfortable with dictionaries as you will use these containers a lot), you can use the code below instead of many if/elif. Whenever you find yourself using many if/elif, if is time to take a look at a more efficient way of laying out the code. drinkChoice=int(input('Enter the number of your item please: ')) ## note that you do not edit for bad input drink_literals=["Not used", 'Coca-Cola selected...Small $1.50...Medium $3.00...Large $4.50', 'Root Beer selected...Small $1.25...Medium $2.50...Large $3.75', 'Bottle Water selected...Small $1.75...Medium $3.50...Large $5.25', 'Apple Juice selected...Small $2.00...Medium $4.00...Large $6.00'] print("\n", drink_literals[drinkChoice], "\n") ## break --> nothing to break from RE: beverage machine code - ccm1776 - Sep-27-2018 Thank you for your replies. I will look into dictionaries for sure. This is the last line of my code here, so when I get here to this point I am trying to write the final part which is comparing the user money input to the total sale price [python/] userPayment=float(input('Enter amount of money to be inserted, including dollars and cents $ ')) [python/] after this line I try to write an if statement, for example [python/] userPayment=float(input('Enter amount of money to be inserted, including dollars and cents $ ')) if userPayment > totalPrice: print ('You change is $ ', format(float(userPayment-totalPrice,'.2f')) if userPayment < totalPrice: print ('Sorry, not enough credit') [python/] It gives me an invalid syntax error on the second if statement, and if i change the second line to elif i get the same error RE: beverage machine code - ichabod801 - Sep-28-2018 You are using the python tags wrong. The first one should have no slash, the second one should have a slash before python. You are missing a closing parentheses on the print call on the line before the if. When you see a syntax error at the start of a line, it usually means the problem is on the line before it. RE: beverage machine code - gruntfutuk - Sep-29-2018 You probably want to check for >= rather than just >, and you only need an else clause rather than another if statement. You've missed a closing bracket for the float() call in format. Here's revised code example: totalPrice = 4.36 # for testing purposes userPayment=float(input('Enter amount of money to be inserted, including dollars and cents $ ')) if userPayment >= totalPrice: print ('You change is $ ', format(float(userPayment-totalPrice),'.2f')) else: print ('Sorry, not enough credit') RE: beverage machine code - ccm1776 - Sep-30-2018 What a beginner mistake. Thanks. I have another question. Say I wanted to do something like userCredit=input('Enter money here') print('example 5.00')How do I make both of these lines run? Would I only be able to choose to either put the example in before or within the input string? Otherwise it would go input then print. RE: beverage machine code - ichabod801 - Sep-30-2018 You could do: print('Enter money here') userCredit = input('example 5.00')or userCredit = input('Enter money here\nexample 5.00')Note that \n inserts a new line into a string. Stylistically, I general go with: userCredit = input('Enter money here (for example, 5.00): ') RE: beverage machine code - gruntfutuk - Sep-30-2018 Another option comes to mind from the excellent example @ichabod801 gave. print('Enter money here\n(example 5.00): ', end='') userCredit = input()The end=<somestring> in a print function overrides the default \n that is normally output after everything else output by a print() call.The <somestring> can be pretty much anything (several newlines perhaps). Here, the empty string means that the next bit of output, from the input() function, continues on the same output line. (If there had been any prompting text inside the input() call, that would have appeared first on the same line as the preceding print() output before Python pauses for input.
|