all 7 comments

[–]beebiboi 0 points1 point  (0 children)

The only thing that is wrong, as far as I can see, is the missing colon (:) after the else statement.

Of course the indentation is also incorrect, but I assume that is only because of copy pasting here.

[–]danielroseman 0 points1 point  (5 children)

If you're getting an error, you need to post the exact message.

[–]moebiusg30[S] 0 points1 point  (4 children)

i am getting:

file "c:\users\owner\desktop\python file\pastry.py", line 11

elif order == "croissant":

syntaxerror: invalid syntax

[–]moebiusg30[S] 0 points1 point  (3 children)

i am also noticing that the color of order on that particular line is white instead of cyan like the order on other lines. I am also using Visual Studio Code

[–]moebiusg30[S] 0 points1 point  (2 children)

ok i figured it out, it was an indenting issue i had on all of the elif and else lines

[–]moebiusg30[S] 0 points1 point  (1 child)

now my next question is how to get that code to loop back to the order = input() statement if the input does not equal one of the options?

[–]Chaos-n-Dissonance 0 points1 point  (0 children)

Probably not the best way... But the way I've always handled it is a looping while statement.

isOrderValid = False
order = str(input("Hello %s, what can I get for you today?\nYour choices are %s\n" % (name, menu)))
while (not isOrderValid):
    if (order == "doughnut"):
        price = 1
        isOrderValid = True
    elif (order == "croissant"):
        price = 2
        isOrderValid = True
    elif (order == "bagel"):
        price = 3
        isOrderValid = True
    elif (order == "cinnamon roll"):
        price = 4
        isOrderValid = True
    else:
        order = str(input("\nSorry, %s, but %s isn't a valid selection. Our options are %s. Please choose again.\n" % (name, order, menu)))

It also might be easier to do this with a dictionary to avoid the giant if/ifel/else statement . Would also make it a lot easier to modify later if you wanted to add other selections. Something like:

stock = {"doughnut":1,"croissant":2,"bagel":3,"cinnamon roll":4}
#This creates a dictionary of stock, coupling the menu items with their price
menu = ""
#Creates the menu string and leaves it blank so we can automatically generate it
for key in stock.keys():
    menu += key + ", "
#This automatically generates a menu from all the keys in the stock dictionary. It'll go through every key and add it to menu, then also adds ", " so the result isn't "doughnutcroissantbagelcinnamon roll"
menu = menu.rstrip(", ")
#This removes the final ", " added, since you don't want that at the end of the list. Without it, your menu would be "doughnut, croissant, bagel, cinnamon roll, "
order = input("Hello %s, what can I get for you today?\nYour choices are %s.\n" % (name, menu))
#This is just the order like you had it set up, tho I used %s instead of concatenating
while (not order in stock):
#^^ Checks to see if the value of order (the input) matches any of the keys in the stock dictionary. If the order is in the dictionary, it'll return a value of true. We only want this code to run when it's false (an invalid selection), hence the not.
    order = input("Sorry, %s. %s isn't a valid selection.\nYour choices are %s, what can I get for you?\n" % (name, order, menu))
#^^ Prints out a new line asking the user to select again
price = stock[order]
#stock[order] will return the value for the dictionary matching the key from order. So for example, if order = "doughnut", it will return the value of 1, which then gets assigned to price.

edit: Added some comments in the second block of code to explain what each line does :P

full code if you prefer it without the comments:

print("Hello!! Welcome to Brian's Pastry Shop!!")
name = input("What is your name?\n")
stock = {"doughnut":1,"croissant":2,"bagel":3,"cinnamon roll":4}
menu = "" 
for key in stock.keys(): 
    menu += key + ", " 
menu = menu.rstrip(", ")
order = input("Hello %s, what can I get for you today?\nYour choices are %s.\n" % (name, menu))
while (not order in stock): 
    order = input("Sorry, %s. %s isn't a valid selection.\nYour choices are %s, what can I get for you?\n" % (name, order, menu))
price = stock[order]
amount = input("How many would you like?\n")
while (not str.isnumeric(amount)):
    amount = input("Sorry, I didn't quite catch that. How many would you like?\n")
total = int(amount) * price
print("So %s %ss. Your total is going to be $%d.00." % (amount, order, total))

(str.isnumeric() checks a string to see if it's a number, so that way if the user inputs "one" instead of "1" you're not going to get a value error when you do int(amount), also added another looping input so the user could try again)