# input function returns a string
# n_str = input("n = ")
n_str = "5.6"
n = int(n_str)
print(f"Provided number n = {n} with type = {type(n)}")
ValueError: invalid literal for int() with base 10: '5.6'
PHOT 110: Introduction to programming
LECTURE 06
Michaël Barbier, Spring semester (2023-2024)
Remember that we have different types of errors
Try to make the following files error free:
lecture_11_ex_errors_books.py
lecture_11_ex_errors_runtime.py
Remember the function for division:
def div(number, divisor):
""" divides n/m and returns the quotient and rest """
quotient = number // divisor
remainder = number % divisor
return quotient, remainder
print("Division of n/m:")
n = int(input("\tn = "))
m = int(input("\tm = "))
quotient, remainder = div(n, m)
print(f"Dividing {n} by {m} = {quotient}, rest = {remainder}")
Try to prevent the error by input validation?
Or handle the error “when it already occurred”?
In Python preventing a problem is not always better than treating it !
eval
function to evaluate a string expressionisinstance(object, type)
checks if an object is of typetry
- except
block
try
block are executed until an exception is encounteredexcept
block is executed on encountering an exceptiontry
- except
block
try
block are executed until an exception is encounteredexcept
block is executed on encountering an exceptionlecture_11_ex_errors_validate_input.py
try:
n = int("3"); m = int("5.5")
quotient = n // m
except ZeroDivisionError:
print("Can't divide by zero")
except ValueError:
print("Please provide two integers")
else:
print(f"quotient = {quotient}")
finally:
# Always executed
print(f"You tried to divide n / m")
Please provide two integers
You tried to divide n / m
try:
n = int("5"); m = int("0")
quotient = n // m
except ZeroDivisionError:
print("Can't divide by zero")
except ValueError:
print("Please provide two integers")
else:
print(f"quotient = {quotient}")
finally:
# Always executed
print(f"You tried to divide n / m")
Can't divide by zero
You tried to divide n / m
try:
n = int("6"); m = int("2")
quotient = n // m
except ZeroDivisionError:
print("Can't divide by zero")
except ValueError:
print("Please provide two integers")
else:
print(f"quotient = {quotient}")
finally:
# Always executed
print(f"You tried to divide n / m")
quotient = 3
You tried to divide n / m
else
else
keyword allows to split the error handling of the try
block:
try
-part which you want to catch errors nowelse
-part which you have code that has its own error handling, or should crash if a problem occurs.finally
finally
is used to gracefully crash, examples would be:
sys.argv
lecture_11_ex_command_line.py
argparse
lecture_11_ex_argparse.py
/
symbols (forward slash)\
symbols (backslash)\
separator should be escaped \\
# Problem with file paths in Windows:
file_path_in_windows = "C:\Users\mich\Documents\my_file.txt"
file_path_in_linux = "/home/mich/Documents/my_file.txt"
Python expects /
or \\
as file separator !
/
symbols (forward slash)\
symbols (backslash)\
separator should be escaped \\
with
keywordopen
keywordfinally
<svg version="1.1"
width="400" height="400"
xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="red" />
<circle cx="150" cy="100" r="80" fill="green" />
<text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
</svg>
pynput
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print(f"Mouse position is {mouse.position}")
# Set pointer position
mouse.position = (100, 200)
print(f"Mouse moved to {mouse.position}")
Lecture 11: Input/Output and error-handling