Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

python question, invalid syntax error, when typing in the console python -i expenses.py or python expenses.py

travel_expenses = [
    [5.00, 2.75, 22.00, 0.00, 0.00],
    [24.75, 5.50, 15.00, 22.00, 8.00],
    [2.75, 5.50, 0.00, 29.00, 5.00],
]

print("Travel Expenses:")
week_number = 1
for week in travel_expenses:
      print("* Week #[]:".format(week_number, sum(week)))
      week_number +=1

Moderator edited: Markdown added so that code renders properly in the forums. -jn

What's the error?

the error is a invalid syntax error when putting a command in the console to call the program

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Simran Bansari ! I'm not sure why exactly you're getting the syntax error. Are you doing this in workspaces? When I run it in workspaces, I get no syntax error. This makes me think that perhaps you're doing it on your local system and may be running a different version of Python.

I do see a couple of oddities though. First, you have an extraneous comma in your multidimensional array after the last sub-array. Secondly, your print statement should have two sets of curly braces {}, instead of the one set of square brackets []. I took the liberty of reworking your code so it works like I think you're expecting.

travel_expenses = [
    [5.00, 2.75, 22.00, 0.00, 0.00],
    [24.75, 5.50, 15.00, 22.00, 8.00],
    [2.75, 5.50, 0.00, 29.00, 5.00]
]

print("Travel Expenses:")
week_number = 1
for week in travel_expenses:
      print("* Week #{}: {}".format(week_number, sum(week)))
      week_number +=1

Hope this helps! :sparkles: