forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_input.py
More file actions
41 lines (32 loc) · 1.26 KB
/
Copy pathsafe_input.py
File metadata and controls
41 lines (32 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
"""
Exceptions Lab solution:
The raw_input() function can generate two exceptions:
EOFError or KeyboardInterrupt on end-of-file(EOF) or canceled input.
Create a wrapper function, perhaps safe_input() that returns None rather
rather than raising these exceptions, when the user enters ^C for Keyboard
Interrupt, or ^D (^Z on Windows) for End Of File.
NOTE: if the user types a few charactors, and the hits ctrl+C, the
KeyboardInterrupt gets caught somewhere deeper in the process, and
this function doesn't work.
Don't worry about that -- I don't really understnd what's going on in
the REPL (Read, Evaluate, Print Loop) either -- and the point of this
assigment is simple Exception handling.
"""
def safe_input(prompt_string=""):
"""
print user for input -- returning a string.
This is just like the built-in input(), but it will return None
if the user hits ctrl+c, (or ctrl+D) rather than raise an Exception.
"""
try:
response = input(prompt_string)
return response
except (EOFError, KeyboardInterrupt):
return None
if __name__ == "__main__":
response = safe_input("Say Something: ")
if response is None:
print("Hey! you cancled out!!!")
else:
print("You said:", response)