Python:if,elif,else.错误:语法错误:main.py 第 6 行输入错误



我有一个错误,我正在尝试为我正在从事的项目消除错误。

语法错误:main.py 第 6 行输入错误

我打算在应得的功劳上给予功劳。帮助我解决这个问题的人可以弄清楚他们希望如何在我的代码中接收注释。

我不知道该尝试什么。
编辑:https://trinket.io

if save == 'y':
filename = 'storage.json'
with open(filename, 'w') as f_obj:
json.dump(box, f_obj)
print(storage)
elif save == 'n':
input('another box?')
else:
print('i don't understand your input')

我想在重新打开电源时将变量保存到可检索的存储中。

当用户键入"y"时,变量将保存。

当用户键入"n"时,系统将询问用户是否需要创建另一个框。

当用户键入其他内容时,他们将被告知"我不明白...">

当你有一个 if/elif/else 块时,从if到最终else的所有代码都必须在这些语句下面缩进,如下所示:

if a == 1:
# do this
# and this
elif a == 2:
# do that
# and that
else:
# do the other thing
# and the other thing

但在代码中,with open行和print行不缩进。

你在这里有两个问题:

  • 缩进不正确。
  • 您正在混合引号:"我不..."。所以你的字符串实际上是在 t 之前结束的。将单引号替换为双引号,并保持撇号不变。
if save == 'y':
filename = 'storage.json'
with open(filename, 'w') as f_obj:
json.dump(box, f_obj)
print(storage)
elif save == 'n':
input('another box?')
else:
print('i dont understand your input')

你能试试一次吗? 我在"不要"处收到错误,因为它混淆了打印语句中的引号。

相关内容

最新更新