如何解决无效的语法问题?

时间:2019-10-13 16:14:51

标签: python

我遇到了这个问题,似乎无法找到解决方案。它不断向我显示第15行的无效语法。怎么了?

print("Shirt Order")
print()

colour = input("What colour of the shirt? Blue or White: ")
if colour.lower() == "blue":
    size = input("What size of the shirt? (S/M/L): ")
    if size.lower() == "l":
        print("Sorry, we do not have that size available.")
elif size.lower() == "m":
    print("You have selected a blue shirt in size medium, that will be $9 please.")
elif size.lower() == "s":
    print("You have selected a blue shirt in size small, that will be $5.50 please.")
else:
    print("Sorry, your order was not recognized, please try again.")
elif colour.lower() == "white":
    size = input("What size of the shirt? (S/M/L): ")
    if size.lower() == "l":
        print("You have selected a white shirt in size large, that will be $10 please.")
elif size.lower() == "m":
    print("You have selected a white shirt in size medium, that will be $9 please.")
elif size.lower() == "s":
    print("Sorry, we do not have that size available.")
else:
    print("Sorry, your order was not recognized, please try again.")

2 个答案:

答案 0 :(得分:2)

强烈缩进代码。

这就是您的代码的样子

df['Actual']  = pd.to_datetime(df['Actual'], dayfirst=True)
df['Scheduled']  = pd.to_datetime(df['Scheduled'], dayfirst=True)
df['DIFF_MINUTES'] = (df['Actual']-df['Scheduled']).dt.total_seconds()/60

print(df)
               Actual           Scheduled  DIFF_MINUTES
0 2017-01-01 04:03:00 2017-01-01 04:25:00         -22.0
1 2017-01-01 04:56:00 2017-01-01 04:55:00           1.0
2 2017-01-01 04:36:00 2017-01-01 05:05:00         -29.0
3 2017-01-01 06:46:00 2017-01-01 06:55:00          -9.0
4 2017-01-01 06:46:00 2017-01-01 07:00:00         -14.0

答案 1 :(得分:1)

有两种类型的错误:

1]在其他elif和else指令所在的缩进块中未定义size变量

2]在第15行中,抛出此语法错误,因为不应将elif控制放在其他位置。否则,应始终将其放在if-elif链的最后。

您应该在if-elif链的最后放置其他符号,以便语法正确