176k views
5 votes
Write a shell (text-based) program, called sum_second.py, that opens a text file called stuff.txt with 2 numbers per line separated by commas and prints out the sum of the second numbers on every line. For example, if stuff.txt has the following lines: 500,55 300,45 200,7 400,20 Then the following example output would happen. PS C:\Users\ssiva\Desktop> python sum_second.py Sum: 127 PS C:\Users\ssiva\Desktop>

1 Answer

4 votes

Answer:

See explaination for the program code

Step-by-step explanation:

The Programming code:

inFile = open("stuff.txt", "r")

num = []

Sum = 0

for val in inFile.readlines():

value = val.split(",")

Sum = Sum + int(value[1])

print("Sum of second number on every line in the file is: ", Sum)

inFile.close()

Please kindly check attachment for output

Write a shell (text-based) program, called sum_second.py, that opens a text file called-example-1
User Mohammad H
by
4.4k points