Write a complete python program, called prog2.py, that will read in integers fro
ID: 3769470 • Letter: W
Question
Write a complete python program, called prog2.py, that will read in integers from the file final.dat. The program should determine the number of the integers read in that are over 5, and then add that number to all the integers read in. for example, if the file final.dat contained the following:
7 1 9 2
4 6 3
the program would print:
The number of over 5 is 3
Adding 3 to all the integers results in the list
10 4 12 5 7 9 6
A sample final.dat file has been placed in your account.
Explanation / Answer
import sys
arr = []
inp = open ("final.dat","r")
#read line into array
for line in inp.readlines():
# loop over the elemets, split by whitespace
for i in line.split():
# convert to integer and append to the list
arr.append(int(i))
c=0
for i in arr:
if i > 5:
c=c+1;
print("The number of over 5 is "+str(c))
print("Adding "+str(c)+" to all the integers results in the list")
for i in arr:
sys.stdout.write(str(i+c)+' ')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.