forked from UWPCE-PythonCert/IntroPython-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive_mainloop.py
More file actions
46 lines (32 loc) · 930 Bytes
/
recursive_mainloop.py
File metadata and controls
46 lines (32 loc) · 930 Bytes
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
"""
recursion in an interactive loop
This code will work -- but not a great idea!
"""
import sys
def mainloop():
while True:
ans = input('type "a", "b", or "quit"')
if ans == "a":
print("you typed a")
elif ans == "b":
second_loop()
elif ans[0] == "q":
print("quitting")
# break
sys.exit() # what if I use the break, rather than the exit()?
elif ans[0] == "r": # here to test recursion...
raise Exception()
# else: # no expected response -- start the loop again
# mainloop()
def second_loop():
while True:
ans = input('type "a", "b", or "go back')
if ans == "a":
print("you typed a")
elif ans == "b":
second_loop()
elif ans[0] == "g":
return
if __name__ == "__main__":
mainloop()