Python Forum

Full Version: Syntax error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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?
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?
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.
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.