minte9
LearnRemember



pag. 44

Errors

What are syntax errors?
What&


Syntax Errors

 
""" Syntax error refers to the structure and the rules
"""

print(1 + 2

"""
    SyntaxError: unexpected EOF while parsing
"""

Runtime Errors

 
""" Runtime errors does not appear until after the program has started
    Those type of errors are called exceptions
"""

first = "Hello"
second = "World"

print(first + " " + secoend)

"""
    NameError: name 'secoend' is not defined
"""

Semantic Errors

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

a = input('Enter number 1: ')
b = input('Enter number 2: ')
sum = a + b

assert sum == (int(a) + int(b)), "Assertion Failed"

"""
    AssertionError: Assertion Failed
"""

Catch Errors

 
""" Use try/except statements to catch errors
"""

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

assert calc(10, 2) == 5
assert calc(10, 0) == None

""" 
    Error: Division by zero
"""



  Last update: 129 days ago