Skip to content

Commit 1fa519b

Browse files
PythonCHBAnastomose
authored andcommitted
added another solution
1 parent 59586db commit 1fa519b

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

Solutions/Session04/copy_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
source, dest = sys.argv[1:3]
1010

11-
infile = open(source, 'rb')
12-
outfile = open(dest, 'wb')
11+
infile = open(source, 'rb') # read binary mode
12+
outfile = open(dest, 'wb') # write binary mode
1313

1414
outfile.write(infile.read())
1515
infile.close()

Solutions/Session04/safe_input.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Exceptions Lab solution:
5+
6+
The raw_input() function can generate two exceptions:
7+
EOFError or KeyboardInterrupt on end-of-file(EOF) or canceled input.
8+
9+
Create a wrapper function, perhaps safe_input() that returns None rather
10+
rather than raising these exceptions, when the user enters ^C for Keyboard
11+
Interrupt, or ^D (^Z on Windows) for End Of File.
12+
13+
NOTE: if the user types a few charactors, and the hits ctrl+C, the
14+
KeyboardInterrupt gets caught somewhere deeper in the process, and
15+
this function doesn't work.
16+
17+
Don't worry about that -- I do'nt really understnd what's going on in
18+
the REPL (Read, Evaluate, Print Loop) either -- and the point of this
19+
assigment is simple Exception handling.
20+
"""
21+
22+
23+
def safe_input(prompt_string=""):
24+
"""
25+
print user for input -- returning a raw string.
26+
27+
This is just like the built-in raw_input(), but it will return None
28+
if the user hits ctrl+c, ratehr than raise an Exception.
29+
"""
30+
try:
31+
response = raw_input(prompt_string)
32+
return response
33+
except (EOFError, KeyboardInterrupt):
34+
return None
35+
36+
if __name__ == "__main__":
37+
response = safe_input("Say Something: ")
38+
print "You said:", response
39+
40+
41+

slides_sources/source/session04.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,13 @@ Nope: the *type* is the problem::
893893

894894
but should you be checking type anyway? (EAFP)
895895

896+
===
896897
LAB
897-
====
898+
===
898899

899900
Exceptions Lab
900901

902+
901903
Exceptions Lab
902904
---------------
903905

@@ -1198,15 +1200,15 @@ In the class repo, in:
11981200

11991201
``Examples\Session01\students.txt``
12001202

1201-
You will find the list I genrated of all the students in teh class, and
1202-
what programming langues they used in the past.
1203+
You will find the list I generated of all the students in the class, and
1204+
what programming languages they have used in the past.
12031205

12041206
Write a little script that reads that file, and generates a list of all
12051207
the languages that have been used.
12061208

1207-
Extra credit: keep track of how many sutdents specified each language.
1209+
Extra credit: keep track of how many students specified each language.
12081210

1209-
If you've got giot set up right, ``git pull upstream master`` should update
1211+
If you've got git set up right, ``git pull upstream master`` should update
12101212
your repo. Otherwise, you can get it from gitHub:
12111213

12121214
``https://github.com/UWPCE-PythonCert/IntroToPython/blob/master/Examples/Session01/students.txt``

0 commit comments

Comments
 (0)