Python Forum
Syntax error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Syntax error (/thread-24876.html)



Syntax error - gray1306 - Mar-08-2020

Hi,

I have a syntax error on line 67 and can't seem to figure out why? can someone please help and explain why please?

# games_database.py

def load_games(filename):
    pass
    
def save_games(filename, games):
    pass

def display_games(games):
    pass

def get_game_from_user():
    pass

def display_menu():
    print()
    print("***Welcome to the Computer Game Database***")
    print()
    print("1. Add new games")
    print("2. Display games")
    print("3. Exit program")
    print()

def main():
    exit_program = False
    while not exit_program:
        display_menu()
        selected_option = int(input("Please select a menu option: "))
        if selected_option == 1:
            import csv

        class game():
            def _init_(self):
                self.name = '-'
                self.platform = '-'
                self.genre = '-'
                self.players = '-'
                self.online = '-'

        #main
        gameList = []
        for count in range (0,3):
            aGame = game()
            aGame.name = input("Enter game name: ")
            aGame.platform = input("Enter game platform: ")
            aGame.genre = input("Enter game genre: ")
            aGame.players = input("Enter number of players: ")
            aGame.online = input("Can you play online?: ")
            gameList.append(aGame)


        #display
        for count in range (0,3):
            print(gameList[count].name)
            print(gameList[count].platform)
            print(gameList[count].genre)
            print(gameList[count].players)
            print(gameList[count].online)
            print("-----------")

        #write to csv
        with open('gameList.csv', 'w', newline='') as file:
            writer = csv.writer(file)
            for count in range(0, 3):
                writer.writerow([gameList[count].name, gameList[count].platform, gameList[count].genre, gameList[count].players, gameList[count].online])

        elif selected_option == 2:
                gameList = list()

                filename = 'gameList.csv'
                with open (filename) as fin:
                    for line in fin:
                        gameList.append(line)
                gameList.sort()
                print(gameList)

        elif selected_option == 3:
            pass
        else:
            print("Please enter a valid option (1-3)")
            print()

if __name__ == "__main__":
    main()



RE: Syntax error - ibreeden - Mar-08-2020

Hello Gray1306,
It is always helpful to post the full errormessage. It always contains important information. But in this case: the error is on elif. So look: where is the if it belongs to?


RE: Syntax error - gray1306 - Mar-08-2020

hi the error message is;

File "c:\users\windows\mu_code\games_database1.py", line 67
elif selected_option == 2:
^
SyntaxError: invalid syntax
>>>

and the if is on line 29, is it to do with the indent?


RE: Syntax error - ibreeden - Mar-08-2020

Please use BBcode. There is a button to insert error messages.
As I said before: there is no if before the elif. That is the error.


RE: Syntax error - deanhystad - Mar-08-2020

Yes, it has to do with indents. Your code is doing this:

if (selected_option == 1)
{
import csv;
}

All the stuff between lines 31 through 66 is outside the "if" statement because of how it is indented.