Python Forum

Full Version: Syntax error with def ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

I am in chapter 5 of Automate the Boring Stuff with Python and working on coding the guest list problem in the chapter. For reference, I am using mu-editor with my coding stuff. I keep getting a syntax error for the def totalBrought (line 3) and cannot for the life of me figure out what I did wrong. I am including an the code below for reference. Please keep in mind I am a SUPER newb so I'm sure I am overlooking something super easy and simple. Any help or guidance would be much appreciated!

allGuests={'Alice': {'apples':5, 'pretzels': 12}, {'Bob':{'ham sammies': 3, 'apples':2}, 'Carol': {'cups': 3, 'apple pies':1}}
def totalBrought(guests,item):
numBrought=0
for k, v in guests.items():
numBrought=numBrought+v.get(item,0)
return numBrought

print('Number of things being brought:')
print('-Apples '+str(totalBrought(allGuests, 'apples')))
print(' -Cups '+str(totalBrought(allGuests, 'cups')))
print(' -Cakes '+str(totalBrought(allGuests, 'cakes')))
print(' -Ham Sammies '+str(totalBrought(allGuests, 'ham sammies')))
print(' -Apple pies '+str(totalBrought(allGuests, 'apple pies')))

Here's the output I am getting:

def totalBrought(guests,item):
^
SyntaxError: invalid syntax

Thank you!
Very likely the error is in the line before. Check to make sure all your parentheses and quotes are balanced and you haven't left any out.
Thank you! I had one too many curly brackets before 'Bob' in the first line that shouldn't have been there! I appreciate you taking the time to look/answer!

(Aug-25-2020, 02:36 AM)bowlofred Wrote: [ -> ]Very likely the error is in the line before. Check to make sure all your parentheses and quotes are balanced and you haven't left any out.