TransWikia.com

elif a=="no": ^ SyntaxError: invalid syntax

Stack Overflow Asked by Mayar Kurdi on November 29, 2021

this is the error i get for my code I cant seem to find out how to fix it

File "C:UsersmayarDesktopfinal edge projectexecute_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")

3 Answers

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

Answered by user13801689 on November 29, 2021

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

Answered by Krishnan Shankar on November 29, 2021

You need to have elif directly after if. You can't have any other lines of codes between an if and the following elifs. 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.

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

Answered by Abhishek Bhagate on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP