Java Python CSC1034: Lecture 2
Intro
• This weeks programming topics:
– Errors
– Reading and Writing
• This weeks development topics:
– Debugging
– Dependencies
• Exercises
Errors
• In life, things often break
• In programming, things break even more often
• How do we deal with errors?
Errors
• Fundamental Restrictions:
• If something is wrong, break!
• Don’t crash, Do Error
• Fail Early
Errors
• Error Handling and Main Logic should be seperable
• Might want to behave differently at different times
• Will see a similar thing with reading/writing later!
Demonstration
Potato!
Errors
• Python uses an error mechanism called exceptions
## Status: Crash
10 * (1/0)
Traceback (most recent call last):
File "/home/phillord/documents/teaching/2023-24/csc1034/dev-repo/lectures-1/python/except
10 * (1/0)
ZeroDivisionError: division by zero
Errors
• An error message in detail
Traceback (most recent call last):
File "exceptions.py", line 5, in <module> 10 * (1/0)
ZeroDivisionError: integer division or modulo by zero
• Traceback – where the error came from
• What the error was
• And a description
Errors
• Trackback trivial in the last case
• Here is a more useful one
## Status: Crash
def fun1():
10/0
def fun2(): fun1()
def fun3(): fun2()
fun3()
Traceback (most recent call last):
File "/home/phillord/documents/teaching/2023-24/csc1034/dev-repo/lectures-1/python/except fun3()
File "/home/phillord/documents/teaching/2023-24/csc1034/dev-repo/lectures-1/python/except fun2()
File "/home/phillord/documents/teaching/2023-24/csc1034/dev-repo/lectures-1/python/except fun1()
File "/home/phillord/documents/teaching/2023-24/csc1034/dev-repo/lectures-1/python/except
10/0
ZeroDivisionError: division by zero
Errors
• Errors can be caught
try :
print(10/0)
except ZeroDivisionError :
print("An attempt was made to divide by zero")
Errors
• Errors can percolate
def deeper_error_prone(): print(10/0)
def error_prone():
return deeper_error_prone()
try :
error_prone()
except ZeroDivisionError :
CSC1034: Lecture 2Python
print("An attempt was made to divide by zero")
Errors
• Python errors are really quite good
• Get used to reading the error messages
Reading and Writing
• At some level, most python programmes need to read or write
• Otherwise, the program will do the same thing every time
Reading and Writing
• Python Reads and Writes to streams
• A stream is a general programming concept
Demonstration
Curry or Pizza
Writing
• Python writes to streams
• A stream is a general programming concept
• They are provided by the operating system
• By default, every program has three
– Standard Input
– Standard Output
– Standard Error
• Standard Input is (normally) the keyboard (i.e. what you type)
• Standard Output is (normally) the screen (i.e. what you see)
• Standard Error is (normally) the screen also
• “Normal” output goes to standard output
• “Error” output goes to standard error
Writing
• The main mechanism for writing is the print function
• The help string for print looks like this:
Help on built-in function print in module builtins:
print( . . .)
print(value, . . . , sep= ' ' , end= '\n ' , file=sys.stdout, flush=False)
Prints the values to a stream, or to sys .stdout by default . Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys .stdout . sep: string inserted between values, default a space .
end: string appended after the last value, default a newline . flush: whether to forcibly flush the stream .
Writing
• For Richer control of the output, use the format method
• format is a string method and can be used anywhere
• It uses {} as place holders which are interpolated
print( "Hello, {} and {} . " .format( "John" , "Paul")) Hello, John and Paul .
Writing
• If you’re values are in variables, use f-strings
• New in Python 3.6, so should work most places
john = "John" paul = "Paul"
print(f"Hello, {john} and {paul} . ") Hello, John and Paul