What is wrong with the syntax with line 7 in my code? It says my print statement has invalid syntax and I can't figure it out?

大兔子大兔子 提交于 2021-01-19 10:01:10

问题


total_cost_of_food = float(input('How much did the food cost? '))
sales_tax = 8.25
tax_plus_total = total_cost_of_food * sales_tax / 100 
print("Your tax for this meal was:", tax_plus_total)
x = bool(input("Would you like to add a tip? ")
    if x is False
        print("Thanks, your total for today is:", tax_plus_total)
    else
        print("That is", x, "I would like to add a tip")

I keep getting this error for my print statement that is under the if statement, it says it's a syntax error but I don't see anything wrong with the syntax... I have only been coding for about a week though so I am likely missing something.


回答1:


The : is missing, at the end of the if line and the else line

Instead of if x is False, you should use if not x:

Also, you should reduce the indenting of all four lines in your if-else construct, by one level.




回答2:


bool(input("Would you like to add a tip? ") lacks the second closing parenthesis.

Python very often prints a syntax error for the next line or even a subsequent line after that when you have a missing closing parenthesis. It tries to make sense of what you wrote and it takes a while for it to figure out that it really can't.

This also masks the indentation error on the following line, and the lack of a colon at the end of the if and else lines.



来源:https://stackoverflow.com/questions/65284656/what-is-wrong-with-the-syntax-with-line-7-in-my-code-it-says-my-print-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!