Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A file nums.txt has one number on each line: 4, 9, 2, 7. Write a program that makes the file, reads it back, and shows the total, the biggest, and how many numbers there are.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Write the file with a loop, read it line by line, convert each line with int, and collect the numbers in a list.
Check. The total is 4 + 9 + 2 + 7 = 22, the biggest is 9, and there are 4 numbers. (int("4\n") works: int ignores the white space at the ends.)
Answer: 22 9 4.
Log in to join the discussion.
Loading…