python - elif a=="no": ^ SyntaxError: 无效语法
问题描述
这是我为我的代码得到的错误我似乎无法找到如何解决它
File "C:\Users\mayar\Desktop\final edge project\execute_html.py", line 19
elif a=="no":
^
SyntaxError: invalid syntax
代码 -
import codecs
import subprocess
import os
while (True):
corona = input("Do you want to know more about Coronavirus-COVID-19? answer in yes/no format \n")
if corona== "yes":
url = "CORONAVIRUS.htm"
#Python executes code following the try statement as a “normal” part of the program
try:
os.startfile(url)
#The code that follows the except statement is the program’s response to any exceptions in the preceding try clause.
except AttributeError:
try:
subprocess.call(['open', url])
except:
print('Could not open URL')
break
elif a=="no":
print("Have a nice day!")
else:
print("Enter either yes/no")
解决方案
您需要elif
直接在if
. elif
在 if 和以下s之间不能有任何其他代码行。您可能弄乱了代码中的缩进。您可以编辑您的代码,try except
使其必须包含在第一if
条语句中,然后它将起作用。
正确的代码 -
import codecs
import subprocess
import os
while (True):
corona = input("Do you want to know more about Coronavirus-COVID-19? answer in yes/no format \n")
if corona== "yes":
url = "CORONAVIRUS.htm"
try:
os.startfile(url)
#The code that follows the except statement is the program’s response to any exceptions in the preceding try clause.
except AttributeError:
try:
subprocess.call(['open', url])
except:
print('Could not open URL')
break
elif corona=="no":
print("Have a nice day!")
else:
print("Enter either yes/no")
推荐阅读
- python - 重塑和查看 Numpy ndarray 数据所有权问题
- java - Java代理https实现
- excel - 值没有写入单元格
- angular - 我如何更新 Angular 表单数组
- jquery - 小 jQuery 函数杀死 Infusionsoft 订单表单上的其他函数
- javascript - 使用JS的数学表达式四舍五入
- javascript - 试图使文本出现在“鼠标悬停”上
- ruby - Buildpacks Pack:将 Bundler 配置传递给 Buildpack
- javascript - 如何使用 vanilla Javascript 添加 classList?
- c++ - 从 .csv 文件中读取