forked from Ada-C18/git-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_b.py
35 lines (30 loc) · 913 Bytes
/
function_b.py
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
# Taken From
# Iterating Over Data
# Problem-Set While Loops #11
def silly_sum():
""" reads numbers from the user (use input_int)
summing as we go until either
the user enters 0, or
the sum reaches or exceeds 1000
"""
sum = 0
while sum < 1000:
num = input_int()
sum += num
if num == 0:
break
return sum
def input_int(*args):
# loop forever (until we get valid input)
while True:
# get string input
user_input = input(*args)
try:
# try converting to an int (will return if successful)
return int(user_input)
# if the conversion succeeded, we will have returned already
except ValueError:
# could not convert the input, so show an error message
print('Try again.')
if __name__ == "__main__":
print(f"Answer = {silly_sum()}")