A syntax error is a type of mistake that occurs when writing or interpreting code. It violates the rules of the programming language, causing the program to fail during compilation or execution.
Example
print("Hello, world!)
Common Errors
Missing or mismatched parentheses, brackets, or quotes.
Improper indentation or inconsistent use of tabs and spaces.
Invalid variable names (e.g., starting with a number, containing spaces).
Using reserved keywords as variable names.
Incorrect or missing punctuation, such as colons or commas.
Missing or extra operators (e.g., == vs = for comparison and assignment).
# What error type will this code result in?
name = input('What's your name?') if name == 'bob': print('Nice to meet you, bob')
{"answers": {"A": "Syntax", "B": "Logical", "C": "No error", "D": "Runtime"}, "question_text": "# What error type will this code result in?\n\nname = input('What's your name?')\nif name == 'bob':\n print('Nice to meet you, bob')", "correct_answer": "A"}
What is a syntax error in programming?
{"answers": {"A": "An error caused by incorrect use of programming language rules that prevents the code from compiling or running.", "B": "An error that occurs when a program violates the rules of a specific programming language.", "C": "An error that causes a program to crash or stop running unexpectedly.", "D": "An error that occurs when a program produces unexpected or incorrect results during execution."}, "question_text": "What is a syntax error in programming?", "correct_answer": "B"}
Which type of error occurs when a program violates the rules of a specific programming language?
{"answers": {"A": "Runtime error", "B": "Semantic error", "C": "Syntax error", "D": "Logical error"}, "question_text": "Which type of error occurs when a program violates the rules of a specific programming language?", "correct_answer": "C"}
In programming, why might a developer choose to suppress error and advice messages during program execution?
{"answers": {"A": "To hide all errors from the user, including critical ones.", "B": "To allow the program to continue running without interruption despite minor issues.", "C": "To reduce confusion and distractions caused by non-critical messages during execution.", "D": "To improve program performance by skipping error checks."}, "question_text": "In programming, why might a developer choose to suppress error and advice messages during program execution?", "correct_answer": "C"}
Runtime errors
Runtime errors occur during the execution of a program (while it is running, hence the phrase runtime). These errors cause the program to terminate abnormally.
Example
mylist = [1,2,3,4] for i in range(len(mylist)): print(mylist.pop(i))
Common runtime errors include
Division by zero (ZeroDivisionError).
Accessing an index that's out of range (IndexError).
Trying to convert an invalid string to a number (ValueError).
Accessing a non-existent key in a dictionary (KeyError).
Opening a file that doesn't exist (FileNotFoundError).
Trying to perform an unsupported operation (e.g., appending to an integer).
In Python, what type of error occurs when you type input)) instead of input()?
{"answers": {"A": "Logical error", "B": "Name error", "C": "Runtime error", "D": "Syntax error"}, "question_text": "In Python, what type of error occurs when you type input)) instead of input()?", "correct_answer": "D"}
When does a syntax error occur in a Python program?
{"answers": {"A": "During program debugging", "B": "When the program is parsed before execution", "C": "During program execution", "D": "During program compilation"}, "question_text": "When does a syntax error occur in a Python program?", "correct_answer": "B"}
What type of error occurs when a program's logic produces incorrect results?
{"answers": {"A": "Syntax error", "B": "Semantic error", "C": "Logical error", "D": "Runtime error"}, "question_text": "What type of error occurs when a program's logic produces incorrect results?", "correct_answer": "C"}
Logical Errors
Logical errors occur when the code runs without any syntax or runtime errors, but it does not produce the expected output or behavior. These errors arise due to flaws in the algorithm or incorrect logical reasoning.
Detecting and fixing logical errors can be challenging as the code technically functions correctly, but the desired outcome is not achieved.
Example
num1 = 10 num2 = 5 sum = num1 - num2 # Incorrect operation for addition print("Sum:", sum)
Common errors
Incorrect order of operations in mathematical calculations.
Using the wrong operator (e.g., using + for subtraction).
Off-by-one errors in loops or array indices.
Incorrect handling of conditional statements.
Misunderstanding boolean logic (e.g., using and when or is intended).
In Python programming, what is a logical error?
{"answers": {"A": "An error detected by the Python interpreter during compilation", "B": "An error caused by incorrect syntax that prevents the program from running", "C": "An error that occurs when the program runs but produces unintended results", "D": "An error caused by missing or incorrect runtime resources"}, "question_text": "In Python programming, what is a logical error?", "correct_answer": "C"}
Which type of error is caused by incorrect if-else conditions?
{"answers": {"A": "Semantic error", "B": "Logical error", "C": "Runtime error", "D": "Syntax error"}, "question_text": "Which type of error is caused by incorrect if-else conditions?", "correct_answer": "B"}
What is the main difference between syntax errors and logical errors in programming?
{"answers": {"A": "Syntax errors occur when the code violates the language rules, preventing the program from running; logical errors occur when the program runs but produces incorrect results.", "B": "Syntax errors happen only during runtime, while logical errors are detected during compilation.", "C": "Syntax errors are caused by incorrect program logic, while logical errors are caused by missing punctuation.", "D": "Syntax errors are difficult to identify, while logical errors are easy to detect and fix."}, "question_text": "What is the main difference between syntax errors and logical errors in programming?", "correct_answer": "A"}
Independent Research Task
Objective
Investigate the most expensive programming errors in terms of
Money â Financial damages/losses
Liberty â Jail time or legal consequences
Lives â Fatal consequences due to software failures our
Task
Find real-world examples of programming errors that fall into each of these categories:
Syntax Errors (e.g., mistyped code that halted systems)
Runtime Errors (e.g., crashes due to unhandled exceptions)
Logical Errors (e.g., flawed algorithms that seemed correct but had disastrous outcomes)
Summarize each case
What happened? What was the error? What was the impact (money, liberty, deaths)? Could it have been prevented?
Review: Fill in the Blanks
A is a mistake that violates the rules of the programming language, leading to failure during or execution. Common causes of syntax errors include missing or mismatched , improper indentation, and using reserved as variable names.
occur during the execution of a program, causing it to terminate . Examples of runtime errors include division by , accessing an out-of-range index in a list, and trying to convert an invalid string to a .
happen when the code runs without syntax or runtime errors but does not produce the expected . These errors are often due to flaws in the algorithm or incorrect logical , making them challenging to detect and fix.
For an independent research task, students are encouraged to investigate programming errors that have significant consequences, categorized by financial damage, legal issues, and loss of . They should summarize real-world examples of syntax, runtime, and logical errors, detailing what happened, the nature of the error, and its .
Click the keywords below to fill in the blanks:
Output
Life
Impact
Abnormally
Number
Parentheses
Logical Errors
Zero
Syntax Error
Runtime Errors
Keywords
Reasoning
Compilation