代做CSC1034: Lecture 2帮做Python编程
- 首页 >> WebCSC1034: 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 :
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 .
Writing
• The file argument says which stream to print to
• This could be the screen, a file, a network connection
• sep and end control some of the output
## Status: Shell
print(1 , 2 , 3 , 4 , 5)
print(1 , 2 , 3 , 4 , 5, sep= ' : ' )
print(1 , 2 , 3 , 4 , 5, sep= ' ' )
print(1 , 2 , 3 , 4 , 5, sep= ' fred ' )
print(1) print(2) print(3)
print(1, end= ",") print(2, end= ",") print(3)
>>> ## Status: Shell
>>> print(1, 2, 3, 4, 5)
1 2 3 4 5
>>> print(1, 2, 3, 4, 5, sep= ' : ' )
1:2:3:4:5
>>> print(1, 2, 3, 4, 5, sep= ' ' )
1 2 3 4 5
>>> print(1, 2, 3, 4, 5, sep= ' fred ' ) 1fred2fred3fred4fred5
>>> print(1) 1
>>> print(2)
2
>>> print(3) 3
>>> print(1, end=",")
1,>>> print(2, end=",") 2,>>> print(3)
3
Debugging
• Programming is hard
• It is very easy to get things wrong
• Programming rapidly and efficiently is about:
– Finding out that things are wrong
– Fixing them as quickly as possible
• How well you program, depends on how well you do this
Debugging Session
def minimum(lst): lowest = 0
for i in lst:
if i < lowest:
lowest = i return lowest
Debugging Session
def minimum(lst): lowest = 0
for i in lst:
if i < lowest:
lowest = i return lowest
def test_minimum(expected, lst):
print( "Should be" , expected, ":" , minimum(lst))
test_minimum(-100, [-100 , 0 , 100])
test_minimum(-100, [100 , 0 , -100])
test_minimum(-1000, [-1000 , 0 , 1000]) test_minimum(-10, [-10 , 0 , 10])
test_minimum(1000 , [1000 , 10000]
Should be -100 : -100
Should be -1000 : -1000
Should be -10 : -10
Should be 1000 : 0
Debugging Session
def minimum(lst): print( "here 1") lowest = 0
for i in lst:
if i < lowest:
lowest = i return lowest
def test_minimum(expected, lst):
print( "Should be" , expected, ":" , minimum(lst))
test_minimum(-100, [-100 , 0 , 100])
test_minimum(-100, [100 , 0 , -100])
test_minimum(-1000, [-1000 , 0 , 1000]) test_minimum(-10, [-10 , 0 , 10])
test_minimum(1000 , [1000 , 10000])
Should be -100 : -100
here 1
Should be -100 : -100
here 1
Should be -1000 : -1000
here 1
here 1
Should be 1000 : 0
Debugging Session
def minimum(lst):
print( "Here 1:" , lst) lowest = 0
for i in lst:
print( "Here 2:" , i) if i < lowest:
print( "Here 3:" , i) lowest = i
print( "Here 4, Lowest:" , lowest) return lowest
def test_minimum(expected, lst):
print( "Should be" , expected, ":" , minimum(lst))
#test_minimum(-100, [-100, 0, 100])
#test_minimum(-100, [100, 0, -100])
#test_minimum(-1000, [-1000, 0, 1000]) #test_minimum(-10, [-10, 0, 10])
test_minimum(1000 , [1000 , 10000])
Here 1: [1000, 10000] Here 2: 1000
Here 4, Lowest: 0 Here 2: 10000
Here 4, Lowest: 0 Should be 1000 : 0
Dependencies
• Dependencies are one of the most useful things
• Dependencies are one of the most painful things