June 2024
Beginner to intermediate
908 pages
15h 59m
English
Suppose there is a file named “f_data40.1-1.txt” that contains 10 3-digit integers (separated by a single space character). An example of the structure of the file is shown here.
Write a Python program that calculates and displays their sum.
Solution
There are two approaches to iterate through the numbers of this file.
First approach
In this approach, a string variable named values gets the content of the whole line of the file. Then, the slicing mechanism is used in a for-loop to split the content into individual three-digit numbers. These numbers are then converted to integers using ...
Read now
Unlock full access