Home > Net >  I don't understand why the syntax error is there as this same syntax was used previously
I don't understand why the syntax error is there as this same syntax was used previously

Time:11-20

I do not understand why the syntax error is there as this same syntax was used previously. The syntax error is:

  File "main.py", line 11
    else size == L or l:
         ^^^^
SyntaxError: expected ':'
print("Welcome to Python Pizza Deliveries!")
size = input("What size pizza do you want? S, M, or L ")
add_pepperoni = input("Do you want pepperoni? Y or N ")
extra_cheese = input("Do you want extra cheese? Y or N ")

pizza = int(0)
if size == S or s:
    pizza  = 15
elif size == M or m:
    pizza  = 20
else size == L or l:
    pizza  = 25

if add_pepperoni == Y or y:
    if size == S or s:
        pizza  = 2
    else size == M or m or L or l:
        pizza  = 3

if extra_cheese == Y or y:
    pizza  = 1

print(f"Your final bill is: ${pizza}.")


#File "main.py", line 11
#    else size == L or l:
     ^
#SyntaxError: invalid syntax

CodePudding user response:

You need to replace the S or s with:

if size == 'S' or size == 's':
    #do something
elif size == 'M' or size == 'm':
    #do something
...

Or, you could even just lowercase the input strings and do:

if size == 's':
    #do something
elif size == 'm':
    #something else
...

First of all, S is not a variable, it needs to be a string, and secondly, the or s part won't work. The or s part is converting the variable s to a boolean value. What you need to do is compare size with 's' and 'S'.

CodePudding user response:

Thank you to those who helped. I figured out what was wrong and will put it here in simple terms to hopefully help someone else. Two things to remember.

  1. When doing the size = s you need to put it in quotes since it came in as a string.

  2. You can either do an "elif {condition}:" or an "else:" not an "else {condtion}:"

  • Related