For this function I am supposed to read a file with 12 random numbers. Then I am supposed to output the numbers 1 per line and finally the program is supposed to separate the even numbers and the odds then add them up and display their totals. The problem here is that even though I am getting printing of the numbers fine, the total function in the end is messing up and giving incorrect totals.
def main():
infile = open('numbers.txt','r')
line1 = infile.readline()
line2 = infile.readline()
line3 = infile.readline()
line4 = infile.readline()
line5 = infile.readline()
line6 = infile.readline()
line7 = infile.readline()
line8 = infile.readline()
line9 = infile.readline()
line10 = infile.readline()
line1 = line1.rstrip('\n')
line2 = line2.rstrip('\n')
line3 = line3.rstrip('\n')
line4 = line4.rstrip('\n')
line5 = line5.rstrip('\n')
line6 = line6.rstrip('\n')
line7 = line7.rstrip('\n')
line8 = line8.rstrip('\n')
line9 = line9.rstrip('\n')
line10 = line10.rstrip('\n')
print(line1)
print(line2)
print(line3)
print(line4)
print(line5)
print(line6)
print(line7)
print(line8)
print(line9)
print(line10)
line = infile.readline()
total = 0
evenTotal = 0
oddTotal = 0
while line != '':
total += int(line)
if int(line) % 2 == 0:
evenTotal += int(line)
else:
oddTotal += int(line)
line = infile.readline()
print("=======================================")
print('The total for the even numbers is', evenTotal)
print("=======================================")
print('The total for the odd numbers is', oddTotal)
infile.close()
main()
Here's are the contents from the file
47
64
67
40
91
98
82
2
42
84
48
96
I am only getting 0 for both the totals somehow.
Can someone help with this?
totalyou don't need bothevenTotalandoddTotalsinceoddTotal == total - evenTotal:)