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
  Syntax error for "root = Tk()" dlwaddel 15 1,006 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 333 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,134 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,248 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 847 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,775 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,287 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,893 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