Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error
#1
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()
Reply
#2
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?
Reply
#3
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?
Reply
#4
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.
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,015 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 335 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,454 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,251 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 848 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,778 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,291 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,896 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020