Python Forum

Full Version: Why do i have invalid syntax on a line after print, i see no error ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
main_site = ['mayo', 'ketchup', 'mustard', 'milk', 'eggs']
off_site = ['cocoa', 'flour', 'salt', 'bread', 'butter']
purchases = ['ketchup', 'mustard', 'bread', 'butter']

for purchase in purchases:
    if purchase in main_site or purchase in off_site:
        print("That item is in stock!")
    if purchase in main_site:
        print("The item can be picked up today."
    if purchase in off_site:
        print("The item can be delivered in two to five days.")
    else:
        print("Sorry, that item is not in stock."
print("We hope you purchase from us again!")
It seems to me like it should work. It points to line 10 "if purchase in off_site:" with a syntax error. I don't see it though.

Am I not allowed to use multiple if statements in sequence?

Nevermind. I just noticed I missed some parentheses in a different line.

The program ran when I added the parentheses.

Why didn't it point to the broken print statement though? It pointed to the line below it.
Line 9
        print("The item can be picked up today."
is missing a ) from the end
Edit:
Line 13 has the same problem.
I wish I could just delete the thread. Sorry!
It goes to the next line in case the print statement is carried on in the next line but it finds something it cant print.
        print("The item can be picked up today."
        "on another line")
Thank you. That makes sense.

I have another question now:

main_site = ['mayo', 'ketchup', 'mustard', 'milk', 'eggs']
off_site = ['cocoa', 'flour', 'salt', 'bread', 'butter']
purchases = ['ketchup', 'mustard', 'bread', 'butter', 'hot dogs']

print("\n")
for purchase in purchases:
    print('\n')
    print(purchase)
    if purchase in main_site or purchase in off_site:
        print("That item is in stock!")
    if purchase in main_site:
        print("The item can be picked up today.")
    if purchase in off_site:
        print("The item can be delivered in two to five days.")
    if purchase not in main_site and purchase not in off_site:
        print("Sorry, that item is not in stock.")
print("We hope you purchase from us again!")
main_site = ['mayo', 'ketchup', 'mustard', 'milk', 'eggs']
off_site = ['cocoa', 'flour', 'salt', 'bread', 'butter']
purchases = ['ketchup', 'mustard', 'bread', 'butter', 'hot dogs']

print("\n")
for purchase in purchases:
    print('\n')
    print(purchase)
    if purchase in main_site or purchase in off_site:
        print("That item is in stock!")
    if purchase in main_site:
        print("The item can be picked up today.")
    if purchase in off_site:
        print("The item can be delivered in two to five days.")
    else:
        print("Sorry, that item is not in stock.")
print("We hope you purchase from us again!")
Example 1 works as expected.

Example 2 says the first two purchases are both in stock and not in stock. Why is the "else" statement working unreliably?

I'm guessing else only works with the last if statement, not every if statement in the for loop?
if purchase in off_site:
    print("The item can be delivered in two to five days.")
else:
    print("Sorry, that item is not in stock.")
is not effected by the other if's.
else will happen anytime purchase is not in off_site.

if you only want one thing to evaluate true you can use elif
if something:
    do_something()
elif something_else:
    do_something_else()
else:
    ok_do_this_then()