Exercise 6

Exercise 6

Different types of Syntax errors

Recreate at least three of the errors in your past programs from "onlinegdb" and explain how you solve them. This must be copied and pasted in your assignment sheet,

1 / 14
next
Slide 1: Slide
New lesson editorIlearningdeal@gmail.comFurther Education (Key Stage 5)

This lesson contains 14 slides, with text slides.

Items in this lesson

Exercise 6

Different types of Syntax errors

Recreate at least three of the errors in your past programs from "onlinegdb" and explain how you solve them. This must be copied and pasted in your assignment sheet,

Slide heading

Syntax Error Explained

In Python programming, a Syntax Error occurs when the interpreter encounters a line of code that violates the predefined "grammar" rules of the language. Because Python must parse your code into an understandable format before running it, a syntax error will prevent your entire script from starting.


Slide heading

  • Typical message: SyntaxError: EOL while scanning string literal OR SyntaxError: unexpected EOF while parsing

  • Fix: close the string and the parentheses


python Copy code
print("Hello")

  • Explanation: The code contains characters or token order that Python cannot interpret in the given context (often due to a typo or misplaced punctuation).

  • Example:



  • if True

    print("Yes")


Slide heading

Unmatched closing bracket

  • Explanation: A closing bracket/parenthesis/brace appears without a corresponding opening one (or a mismatch).e text



lst = [1, 2, 3)]


Slide heading

  • Explanation: An opening bracket/parenthesis/brace is never closed by the end of the file.

  • Example:


def f(x):

return (x * 2





Slide heading

  • Explanation: A keyword is used in a place where a valid token is expected, often due to stray text or wrong indentation.



python Copy code


def if(x):
    return x

Slide heading

  • Typical message: IndentationError: expected an indented block

python Copy code


def f():
print("Hi")


Slide heading

  • Explanation: A function or class header ends without the required colon.

  • Example:



python Copy code


def f(x)
    return x



Slide heading

  • Explanation: The code contains characters that aren’t allowed in identifiers (e.g., spaces or non-ASCII letters in a way that isn’t valid in the current encoding).

  • Example:

na me = "Alice"

Slide heading

Logical Error

A logical error is a programming mistake where code runs without crashing but produces incorrect, unintended, or unexpected results. Unlike syntax errors, the program is valid, but its logic or structure is flawed. It is commonly called a "bug," "design error," or "semantic error" (in certain contexts).

Slide heading

#. Take a single mark input from the user

mark = int(input("Please enter the test mark: "))

# 2. Use the three selection commands to interpret the grade

if mark <80:

    print("Grade: A")

elif mark >= 65:

    print("Grade: B")

elif mark >= 50 and mark <= 64:

    print("Grade: C")

else:

    print("Grade: D") 1

Slide heading

Example

if marks <80:

print ("print Grade A")




else:

prinnt ("Grade D")

Here 85 was inputted and Grade D was printed instead of Grade AI

I changed - if marks>80: instead and the code worked

Slide heading

THE END