Reading/Writing Files



Exercise

File input.txt contains numbers separated by comma as shown below,

6,8

7,6

2,8

9,5

9,6

(a) Write a function countNum(file_name,num) such that it returns number of occurrences of a number in that file. for example,

countNum(“input.txt”,9) should return 2

countNum(“input.txt”,100) should return 0

(b) Change input.txt so that when program ends it contains sum of all numbers in a line as shown below,

6,8,sum: 14

7,6,sum: 13

2,8,sum: 10

9,5,sum: 14

9,6,sum: 15


Download Answers