CoCalc Public FilesClass Samples / list_sum.pyOpen with one click!
Author: Ryan McFall
Compute Environment: Ubuntu 20.04 (Default)
1
valid = False
2
3
while not valid:
4
list_size = int(input('How many numbers: '))
5
if list_size <= 0:
6
print ('Size must be positive')
7
elif list_size % 2 != 0:
8
print ('Size must be even')
9
else:
10
valid = True
11
12
numbers = list()
13
number_entered = 1
14
while number_entered <= list_size:
15
number = int(input('Enter number {}: '.format(number_entered)))
16
numbers.append(number)
17
number_entered += 1
18
19
# OR we could also use range to help us count the number of values
20
# entered
21
22
#for index in range(list_size):
23
# number = int(input('Enter number {}: '.format(number_entered)))
24
# numbers.append(number)
25
26
left_total = 0
27
for index in range(0, list_size // 2):
28
left_total += numbers[index]
29
30
right_total = 0
31
for index in range(list_size // 2, list_size):
32
right_total += numbers[index]
33
34
if left_total == right_total:
35
print ("True")
36
else:
37
print ("False")