CoCalc Public FilesClass Samples / list_sum.py
Author: Ryan McFall
Compute Environment: Ubuntu 20.04 (Default)
1valid = False
2
3while 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
12numbers = list()
13number_entered = 1
14while 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
26left_total = 0
27for index in range(0, list_size // 2):
28    left_total += numbers[index]
29
30right_total = 0
31for index in range(list_size // 2, list_size):
32    right_total += numbers[index]
33
34if left_total == right_total:
35    print ("True")
36else:
37    print ("False")