简体   繁体   English

elif a==“no”: ^ SyntaxError: 无效语法

[英]elif a==“no”: ^ SyntaxError: invalid syntax

this is the error i get for my code I cant seem to find out how to fix it这是我为我的代码得到的错误我似乎无法找到如何修复它

File "C:\Users\mayar\Desktop\final edge project\execute_html.py", line 19
    elif a=="no":
    ^
SyntaxError: invalid syntax

Code -代码 -

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")

You need to have elif directly after if .你需要在if之后直接有elif You can't have any other lines of codes between an if and the following elif s.在 if 和以下elif之间不能有任何其他代码行。 You are probably messing up with the indentations in your code.您可能弄乱了代码中的缩进。 You can edit your code to have try except to be included inside the first if statement and then it will work.您可以编辑您的代码以try except包含在第一个if语句中,然后它将起作用。

Correct code -正确的代码 -

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")

Use this code:使用此代码:

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)
        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")

Right code:正确的代码:

import codecs
import subprocess
import os

url = "CORONAVIRUS.htm"
while True:
    corona = input("Do you want to know more about Coronavirus-COVID-19?                                                             answer in yes/no format \n")
    if corona== "yes":
#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 corona=="no":
       print("Have a nice day!")
    else:
        print("Enter either yes/no")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM