Functions and How to Call Them
Python is not intelligent and cannot guess your intent. It is strict about syntax and will reject any code that violates its rules, even by a single character.
A Precise Conversation
When you write Python, you are giving instructions to a language processor. Python does not guess what you intended. It checks whether your instructions follow Python's syntax rules. If they do not, Python rejects the code instead of trying to repair it for you. Even one missing or misplaced character can prevent the line from running.
What a Function Call Looks Like
A function is used through a particular piece of syntax. In the source example, Python uses the print function to display text on the screen. The call begins with the word print, followed by parentheses. The text goes inside the parentheses and is enclosed in quotation marks. Python recognizes this arrangement as a valid instruction and executes it.
Hello world!The important point is not just the word print. Python also needs the surrounding structure: parentheses and matching quotation marks around the text. When all of those parts are present in the expected positions, Python accepts the instruction and produces the text on the screen.
One Character Can Stop Execution
Finding a missing quote
A print call contains text that begins with a quotation mark but does not end with a matching quotation mark.
Inspect the call: The print function call starts correctly, but the text is not closed with the quotation mark Python expects.
Read the error description: Python reports an unterminated string literal. This means it found an opening quote but never found the closing quote needed to finish the string.
Return to the reported line: Use the line number and the pointer shown by Python to locate the area where the problem was detected.
Add the missing character: Place the matching closing quote at the end of the text, then try the instruction again.
The corrected call follows the required pattern: print, parentheses, text enclosed by matching quotes, and a closing parenthesis.
This example demonstrates why syntax errors can feel disproportionate to the mistake. The missing character is small, but Python cannot safely execute a line when it cannot determine where the text ends. The entire line is rejected until the required closing quote is supplied.
Reading SyntaxError Messages
A SyntaxError means that your code violates Python's syntax rules. The message gives information that helps you locate and correct the problem.
- The file and line number where Python encountered the problem.
- The line of code associated with the error.
- A pointer, shown with carets, indicating approximately where the problem was detected.
- The error name, such as SyntaxError.
- A description of what Python believes went wrong, such as unterminated string literal.
The pointer does not mean that the character directly under it is always the original cause. It shows where Python detected that the code no longer fit the rules. Use it together with the line and the description. For an unterminated string literal, the useful investigation is to check the quotation marks on that line and confirm that an opening quote has a matching closing quote.
| Message information | Question to ask |
|---|---|
| Line number | Which line should I inspect first? |
| Code line | What did Python receive? |
| Pointer | Near which location did Python detect the violation? |
| Error name | What category of problem occurred? |
| Description | What rule appears to have been violated? |
The Correction Cycle
Syntax errors are part of learning Python, not evidence that the learning process has failed. The normal process is iterative: write an instruction, let Python accept or reject it, read the error if it is rejected, identify the problem, correct the code, and try again. Repeating this cycle teaches you which forms Python accepts.
When Python reports a syntax error, do not immediately rewrite the whole program. Start with the reported line. Read the error name and description, inspect the nearby punctuation, and then make a focused correction.
Mistakes to Catch Early
Forgetting the parentheses after print.
The source describes the print call as the word print followed by parentheses with the text inside. Leaving out that structure does not match the required syntax.
Fix:
Check that the function name is followed by opening and closing parentheses.Leaving a string without matching quotation marks.
Python cannot determine where the text ends and reports an unterminated string literal.
Fix:
Check every opening quotation mark and add its matching closing quotation mark.Writing natural language instead of a Python command.
Python understands Python syntax, not English sentences, so it rejects the input as invalid Python code.
Fix:
Express the instruction using a recognized Python form, such as a print call with quoted text.Assuming Python will repair almost-correct syntax.
Python is strict about syntax and does not guess the programmer's intent.
Fix:
Use the error message to find the missing or misplaced part and correct it explicitly.
Try the Repair
Imagine that Python rejects a print call because its text begins with a quotation mark but does not end with one. Describe the correction cycle in order: what information would you read first, what part of the line would you inspect, and what would you change before trying again?
Hints
- Start with the line number, error name, and description.
- Inspect the quotation marks on the reported line.
- Look for an opening quote without a matching closing quote.
What do you think happens?
What is the most useful first response when Python reports a SyntaxError?
Reveal answer
Answer: Read the line number and error description, then inspect the reported code.
The error message supplies the location and description needed to identify the syntax violation and make a focused correction.
Working Rule
Calling a function requires exact syntax. For the print function, that means the function name, parentheses, and quoted text in the expected arrangement. If Python rejects the line, treat the SyntaxError as information: locate the line, read the pointer and description, identify the rule you violated, correct the code, and try again.
Python does not guess intent. Correct syntax is the way the programmer communicates instructions that Python can execute.
Key Takeaways
- Python enforces exact syntax and rejects code that violates its rules, even by one character.
- A print function call uses the word print, parentheses, and text enclosed in matching quotation marks.
- A SyntaxError provides a line number, the relevant code, a pointer to the detected location, an error name, and a description.
- Python cannot infer what you meant from natural language or almost-correct syntax.
- The normal learning cycle is to write code, read the error, identify the problem, correct it, and try again.