Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write another program named numbersread.py that uses a loop to read and process

ID: 655193 • Letter: W

Question

Write another program named numbersread.py that uses a loop to read and process numbers.txt. The program should output all of the integers, one per line, and then output the total of the even integers and the total of the odd integers.

This is what I have so far:

import random
def main():
fp = open("numbers.txt", "w")
for i in range(12):
fp.write(str(random.randint(1, 100)) + ' ')
  
main()


def main():
L = []
with open("numbers.txt", "r") as f:
for line in f:
L.append(int(line.strip()))
even = 0
odd = 0
for i in L:
if i % 2 == 0:
even += 1
else:
odd += 1
print (i)
print('Number os Even Numbers is ' + str(even))
print('Number os Odd Numbers is ' + str(odd))

main()

The output doesn't display the list of random intergers.

Explanation / Answer

def main():

infile = open('numbers.txt','r')

evenTotal = 0

oddTotal = 0

line = infile.readline()

while line != '':

if int(line) % 2 == 0:

evenTotal += int(line)

else

oddTotal += int(line)

line = infile.readline()

print('The total for the even numbers is',evenTotal)

print('The total for the odd numbers is',oddTotal)

infile.close()

print('All done!')

main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote