asianpanda
asianpanda

Reputation: 29

How to do syntax errors?

For the program I am doing, it generates a list of anime recommendations by going through a survey style questions and having a CSV file storing info about the anime such as the genre.

However, when running through the program, I wanted to test if the syntax error will occur when doing the questions and ends up at the printing the error after doing all of the questions. What I was hoping it would do is I to have the syntax error will pop up when putting the input incorrectly.

This the what i have done:

if adventure == 'yes':
   with open("AnimeGenre3.csv", 'r', newline='') as f:
       for row in csv.reader(f):
            if row[4] == 'yes':
               anime = str(row[1])
               looping = False
               if anime not in anime_list:
                  anime_list.append(anime)

else:
    print('''
There seems to be a problem in one of the questions
    ''')

Let's say, 'do you like anime', and you enter 6, the syntax error thing pops up and redo the question. But after doing all the questions and generates the list of anime, that's where it pops up.

Another issue is how do I start the program again after finishing the questions and printed the recommended anime. It asks if you want to start again, but it ends up not doing it. This is what I did.

StartAgain = str(input('Do you like to start again; yes or no: '))

if StartAgain == 'yes':
     print('''
Let's begin ''')
break
if StartAgain == 'no':
      print('thank you for coming')

else:
  print('''
 Sorry Man got to do it again''')

 # an if statement if the user wants to start again
 # this should rerun the program.

Now, this is the full program that I have done so far, honestly need to find a way to make it simpler, and hopefully, my annotations are good enough.

 import csv
 anime_list = []
 #in the csv AnimeGenre3.csv it contains information such as the animes, the number of 
 episodes and genres

#printing information about the program.
#has an if statement to identify if it user wants to start the program.

looping1 = True
looping2 = True

print('This is a program that identifies specific anime for you')

while looping1 == True:
begin = str(input('Do you like to start; yes or no: '))

if begin == 'yes':
    print('''
Let's begin
''')
    break
if begin == 'no':
    print('thank you for coming')
else:
    print('''
Sorry Man got to do it again
        ''')
#this section is where the program ask the user questions from there
# it will identify, the genres the user is interested in.

while looping2 == True:

action = str(input('Do you like action; yes or no: '))
adventure = str(input('Do you like adventure; yes or no: '))
print('')
print('recommended anime')
#having to use if statements to find any anime that either say "yes" in the CSV file
# it sorts out all of the sorts that the user said yes to.
# It goes through the rows to identify all of the user genres that say yes
# it should print out the recommend anime
# this prints out a long list of anime

if action == 'yes':
    with open("AnimeGenre3.csv", 'r', newline='') as f:
        for row in csv.reader(f):
            if row[3] == 'yes':
                anime = str(row[1])
                looping = False
                if anime not in anime_list:
                    anime_list.append(anime)
                    
if adventure == 'yes':
    with open("AnimeGenre3.csv", 'r', newline='') as f:
        for row in csv.reader(f):
            if row[4] == 'yes':
                anime = str(row[1])
                looping = False
                if anime not in anime_list:
                    anime_list.append(anime)

else:
    print('''
There seems to be a problem in one of the questions
        ''')
# if there is a syntax error within the program, it should pop up to the user saying this
    
print(anime_list)

StartAgain = str(input('Do you like to start again; yes or no: '))

if StartAgain == 'yes':
    print('''
Let's begin ''')
    break
if StartAgain == 'no':
    print('thank you for coming')

else:
    print('''
Sorry Man got to do it again''')

# an if statement if the user wants to start again
# this should rerun the program.

Upvotes: 1

Views: 74

Answers (1)

themagicbean
themagicbean

Reputation: 156

Regarding starting again, your only code prints a statement. It does not repeat a function. You might want to think about modulating (organizing) your program a bit more so that you can recall a function when you start again. E.g.,

def main()
      //this contains your overall logic, starting/ending the program and/or repeating it
     // at some point in time, you call animeselector() to start, and if it needs repeating, you call it again

 def animeselector ()
      //this controls the in/out and selection process

As far as making sure input is correctly formatted, that could be another function. It could just check if the answer is 'yes' or 'no', and if not, change a variable (like badanswer == false becomes true). Then this triggers something else (the re-do) when you want it to.

I'd recommend checking out Learn Python the Hard Way (later chapters) for function organization, and some PyGame tutorials are also good for seeing how to modulate your programming better (because if making a game, you need an overall function to run the game then a bunch of smaller functions to handle in-game activity).

Upvotes: 1

Related Questions