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
#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


Messages In This Thread
RE: getting a syntax error and i don't know why - by deanhystad - May-11-2021, 10:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  is this really a syntax error? Skaperen 4 384 May-25-2024, 07:31 AM
Last Post: snippsat
  World Clock syntax error OscarBoots 1 337 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 1,789 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 562 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,991 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,455 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,496 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,444 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 1,044 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 2,104 Sep-17-2022, 04:09 AM
Last Post: jttolleson

Forum Jump:

User Panel Messages

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