arr = [] # contains input data
while True:
try: # checking for the correct input data
n = int(input('number: '))
except:
print('An integer must be entered!')
continue
if n == 0: # stopping data entry
break
else:
arr.append(n)
print(arr)
neg_sum, pos_sum = 0, 0
for number in arr: # iterating through all the list elements
if number < 0:
neg_sum += number
pos_sum += number
print('Sum of negative numbers:', neg_sum)
print('Sum of positive numbers:', pos_sum)
Более упрощенный вариант:
if n == 0:
neg_sum = 0
pos_sum = 0
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
arr = [] # contains input data
while True:
try: # checking for the correct input data
n = int(input('number: '))
except:
print('An integer must be entered!')
continue
if n == 0: # stopping data entry
break
else:
arr.append(n)
print(arr)
neg_sum, pos_sum = 0, 0
for number in arr: # iterating through all the list elements
if number < 0:
neg_sum += number
else:
pos_sum += number
print('Sum of negative numbers:', neg_sum)
print('Sum of positive numbers:', pos_sum)
Более упрощенный вариант:
arr = [] # contains input data
while True:
n = int(input('number: '))
if n == 0:
break
else:
arr.append(n)
neg_sum = 0
pos_sum = 0
for number in arr: # iterating through all the list elements
if number < 0:
neg_sum += number
else:
pos_sum += number
print('Sum of negative numbers:', neg_sum)
print('Sum of positive numbers:', pos_sum)