-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_middle_exit.py
More file actions
21 lines (19 loc) · 816 Bytes
/
Copy pathadd_middle_exit.py
File metadata and controls
21 lines (19 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3
''' Adds up a sequence of non-negative integers from user.
User ends list with a negative integer to display the sum of the non-negative
integers. Displays 0 if the user enters no non-negative integers.
'''
# Init
entry = 0 # Control loop
sum = 0
# Get user input
print('Enter numbers to sum up, negative numbers ends list: ')
while True: # Enter loop
try: # Error-check user input
entry = int(input('>>> ')) # Get the Value
if entry < 0: # Check for negative integer
break # If negative, exit loop
sum += entry # Add entry to running sum
except ValueError:
print('Expected integers')
print('Sum =', sum) # Display the sum