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.
Why is python asking for a colon?
hello, I was wondering why the function is asking to put a colon even thought one is already there.
def math(question): if question = 1: answer = num1 + num2 elif question = 2: answer = num1 - num2 elif question = 3: answer = num1 * num2 return answer
Chris FreemanTreehouse Moderator 67,636 Points
Hey Sabeen Azhar, good question.
A colon marks the start of an expected indented block. Since each
else has its own code block, a colon is needed.
elif ended the previous code block by being indented only as far as the previous
**Edit: are you using Python 3.8+? Python might be thing you mean to use the walrus operator. That is, it might be guessing you mean:
if question := 1:
It helps if you post the complete error stacktrace for context.
Post back if you need more help. Good luck!!
The number of colons you currently have in your function is fine.
In the line
if question = 1:, you get a SyntaxError. You probably meant to use
if question == 1: with two equal signs to check equality. The lines starting with
elif have the same issue.
Your function does not have any parameters named
num2, so they might not be defined when the function is run. If you want them to be parameters, you could change your first line to
def math(question, num1, num2):.