Hello, this is Python Day 1, SyntaxError: invalid syntax error question.

Asked 2 years ago, Updated 2 years ago, 21 views

#coffee.py

    coffee=10
    while true:
        money=int("input" please":"))
        if money==300:
            print("Give coffee".")
            coffee=coffee-1
        elif money>300:
            print("Give %d change and give coffee."%(money-300)
            coffee=coffee-1
        else:
            print ("I give you back the money and don't give you coffee.")
            print("The amount of coffee left is %d."%coffee)
        in not coffee:
            print("We are out of coffee". Stop selling.")
            break

An error occurred while running
.
File "/solution.py", line 14
coffee=coffee-1
^
SyntaxError: invalid syntax

coffee=coffee-1 displays an error, but I'm asking you why.

python

2022-09-22 19:26

1 Answers

coffee=coffee-1

First of all, the error that comes from this part comes from the statement right above.

print ("Give %d of change and coffee")% (money-300)

parentheses) missing at the end.

And change true to true and

in not coffee: 

So this is what you were trying to do if not coffee?

Finally, if you modify it as below, it will be executed normally.

coffee=10
while True:
    money=int("input" please":"))
    if money==300:
        print("Give coffee".")
        coffee=coffee-1
    elif money>300:
        print("Give %d change and give coffee."%(money-300))
        coffee=coffee-1
    else:
        print ("I give you back the money and don't give you coffee.")
        print("The amount of coffee left is %d."%coffee)
    if not coffee:
        print("We are out of coffee". Stop selling.")
        break


2022-09-22 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.