Python Forum
getting a syntax error and i don't know why
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting a syntax error and i don't know why
#1
Exclamation 
I've looked at the whole code and can't see what's wrong. please help me it's giving me a head ache!
food = ['donuts', 'pancakes', 'bacon', 'waffles', 'eggs', 'bagels']
score = [0,0,0,0,0,0]

print('Please answer each question with "y" for "yes" and "n" for "no".')
user_input = input('Do you like food with holes? ')
user_input2= input('do you like stuff made from animals? ')
user_input3=input('do you like sweets')
user_input4=input(' ')
if user_input == 'y':
  score[0] = score[0] + 1
  score[5] = score[5] + 1
elif user_input =='n':
  score[0] = score[0]-1
  score[1] = score[1]-1
if user_input2 == 'y':
 score[0] = score[0] + 1
 score[5] = score[1] + 1
elif user_input2 == 'n':
  score[0] = score[0]-1
  score[1] = score[1]-1
if user_input3 == 'y':
 score[0] = score[0] + 1
score[5] = score[5] + 1
elif user_input3 == 'n':
  score[0] = score[0]-1
  score[1] = score[1]-1][
Error:
[File "main.py", line 24 elif user_input3 == 'n': ^ SyntaxError: invalid syntax]
snippsat write May-11-2021, 09:12 PM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
Please edit your post and put your python code in appropriate tags (use the python button above the editing pane). It is probably that the elif statement doesn't have the appropriate indentation to line up with the correct if, but that's too hard to tell without the formatting.
Reply
#3
Missing indentation on line 23.
Generally if you are banging your head against the wall trying to find an error, look one line higher.
Reply
#4
Starting with line 1, there should not be a right square bracket in column 1
[food = ['donuts', 'pancakes', 'bacon', 'waffles', 'eggs', 'bagels']
Square brackets are used to create lists and index collections. You do the prior when creating the list "food", but the first square bracket in column 1 is an error.

You have another bracket error, possibly related?, in the last line:
  score[1] = score[1]-1][
And as already pointed out there are indenting errors in lines 16, 17, 22 and 23. Your indenting should be a consistent 4 spaces per level.
Reply
#5
When your program repeats the same code or pattern over an over you should step back and rethink the logic. The code below takes the repeated code and turns it into a function.
foods = {'donuts':0, 'pancakes':0, 'bacon':0, 'waffles':0, 'eggs':0, 'bagels':0}

def ask(question, good_foods, score=1):
    """Ask quesiton.  If input is 'y', add score to each food item in good_foods"""
    if input(question) == 'y':
        for food in good_foods:
            foods[food] += score

print('Please answer each question with "y" for "yes" and "n" for "no".')

# Add one to the foods if you answer yes to these questions
ask('Do you like food with holes? ', ('donuts', 'bagels'))
ask('Do you like stuff made from animals? ', ('bacon', 'eggs'))
ask('Do you like sweets? ', ('donuts', 'pancakes', 'waffles'))

# Subtract 1 from these foods if you are gluten sensitive
ask('Are you gluten sensitive?', ('donuts', 'pancakes', 'waffles', 'bagels'), -1)
for item, score in foods.items():
    print(item, '=', score)
This code does not implement the same logic as your program. I don't want to accidentally do your homework. But it is full of ideas that you could use to solve your problem.

For example, I used a dictionary instead of a list of food names and a list of food scores. This lets me use the food name as an index instead of using integers. foods['bagel'] is obviously a bagel, but score[5] requires you read the code.

If you aren't writing functions you aren't writing Python. Functions let you make programming language features customized to solving your problem. Sure you can write any program only using the built-in functions, but the code will be much longer and difficult to maintain. In my example 1 simple function lets me replace 7 lines of code in your program.
Marbelous likes this post
Reply
#6
thank you @deanhystad you've answered another problem which is getting the program to print out the variable scores
Reply
#7
 """Ask quesiton.  If input is 'y', add score to each food item in good_foods"""
what does that do? i am guessing that it gives the computer more clarification
Reply
#8
It is a comment line, the computer ignores it, it is for the person reading the code.
Reply
#9
It is a docstring, as in "document" string. The computer collects these and uses them when I ask for help.

I like my "ask()" function so much that I decide to use it in other programs. I reorganize function to make it independent of the foods dictionary.
"""survey: Useful function(s) for writing surveys.
"""

def ask(question, items, yes_items, score=1):
    """Ask quesiton.  If input is 'y', add score to each item in yes_items"""
    if input(question) == 'y':
        for item in yes_items:
            items[item] += score

if __name__ == '__main__':
    # Demo code showing how you would use ask()
    foods = {'donuts':0, 'pancakes':0, 'bacon':0, 'waffles':0, 'eggs':0, 'bagels':0}

    print('Please answer each question with "y" for "yes" and "n" for "no".')
     
    # Add one to the foods if you answer yes to these questions
    ask('Do you like food with holes? ', foods, ('donuts', 'bagels'))
    ask('Do you like stuff made from animals? ', foods, ('bacon', 'eggs'))
    ask('Do you like sweets? ', foods, ('donuts', 'pancakes', 'waffles'))
     
    # Subtract 1 from these foods if you are gluten sensitive
    ask('Are you gluten sensitive? ', foods, ('donuts', 'pancakes', 'waffles', 'bagels'), -1)
    for item, score in foods.items():
        print(item, '=', score)
Now I have a fairly generic function named "ask()" and some code that demonstrates how ask() is used.

I start python, import my module and ask for help.
Output:
>>> import survey >>> help(survey) Help on module survey: NAME survey - survey.py DESCRIPTION Useful functions for writing surveys. FUNCTIONS ask(question, items, yes_items, score=1) Ask quesiton. If input is 'y', add score to each food item in good_foods FILE ...\survey.py
There are also tools that collect all the docstrings from your modules and create nicely formatted user documentation and other kinds of reports.

So to answer you question, the computer does not care what is contained between those triple quotes. That is meant for the user
Reply
#10
o.k i am looking at my code and i got and idea. what if the user is allergic to one the foods in the list. what i want to do is the food to disappear from the list after the input is typed. so how would i do that? And again thank you for taking your time to help me Clap
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a syntax error! rohes_kaugummi 3 326 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 357 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 483 Jun-21-2021, 08:05 AM
Last Post: ibreeden
  Even number code syntax error MrCeez 1 413 May-02-2021, 06:43 PM
Last Post: Larz60+
  error in your SQL syntax Xuli 10 721 May-02-2021, 12:27 PM
Last Post: Xuli
  Function syntax error Changosoyyo 2 558 Mar-07-2021, 10:42 PM
Last Post: Changosoyyo
  Syntax error on line 29 manwithjeans 3 615 Feb-27-2021, 08:28 AM
Last Post: perfringo
  Invalid syntax error - need help fixing calgk01 3 663 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Why do I get Syntax error? TechNitium 1 470 Jan-22-2021, 12:49 PM
Last Post: snippsat
  syntax error CompleteNewb 8 768 Jan-08-2021, 04:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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