(Intro to Python) Assume there is a file called numbers.txt (in the current working directory) with unknown number of integer values on each line. Write a program to read the numbers from the file, add them and then display the total. Use try-except clause to handle errors associated with working with files. This should work for any number of values in the file. If the file does not exist, it should not throw a runtime error. Sample output is,

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 20SA
icon
Related questions
Question

(Intro to Python)

Assume there is a file called numbers.txt (in the current working directory) with unknown number of integer values on each line. Write a program to read the numbers from the file, add them and then display the total. Use try-except clause to handle errors associated with working with files. This should work for any number of values in the file. If the file does not exist, it should not throw a runtime error. Sample output is,

 

Read 6 numbers

They are 5 7 8 3 100 14

Their total is 137

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Basics of loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning