PYTHON

  minte9
REMEMBERS




Syntax

p41 Syntax error refers to the structure and the rules.
 
>>> (1 + 2)  # correct
3
>>> 2)  # incorrect

# SyntaxError: invalid syntax

Runtime

Runtime errors does not appear until after the program has started (also called exceptions).
 
>>> first = "Hello"
>>> second = "World"
>>> first + " " + secoend

# NameError: name 'secoend' is not defined

Semantic

Semantic errors will not generate errors but it will not do the right thing.
 
# Semantic error
#
# The program performs concatenation instead of addition
# The programmer failed to convert the inputs to integers

num1 = input('Enter number 1: ')
num2 = input('Enter number 2: ')
sum = num1 + num2

if sum != int(num1) + int(num2):
    print("Sum = " + sum + " / Incorrect - Semantic error")

num1 = input('Enter number 1: ')
num2 = input('Enter number 2: ')
sum = int(num1) + int(num2)

print("Sum = " + str(sum) + " / Correct")

Except

Use try/except statements to catch errors.
 
# Errors can be handled with ...
# try and except statements.

def calc(number, divider):
    try:
        return number/divider
    except:
        print('Error: Invalid argument')

assert calc(10, 2) == 5
assert calc(10, 0) == None  # pass
    # Error: Invalid argument

Questions     github Language
Slice
        A B C D E F
🔔
1/2