Question

Question 2: Assume that a text file containing a series of integers is named numbers.txt and exists on the c:Temp. Write a program that reads all of the numbers stored in the file and calculates their total. In order to test this program, you may create a text file using notepad. No GUI is necessary. Name the program number.py

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#program numbers.py is given after the line -----------------------------------

Here first you declare a as list , then read each line and split the line into string list then convert each string in string list to numbers ,, each element of numbers is appended to list a and finaly after for loop you print whole list a which is integer list. Pls give +vae rating, thanks

------------------------------------------

a=[]

with open('numbers.txt') as f:

for line in f.readlines():

#print line

numbers = line.split()

int_list = [int(x) for x in numbers]

for i in int_list:

a.append(i)

#print(a)

print("Numbers read from file numbers.txt is: ",a)

----------------------------------------------------------------------------

//numbers.txt file conetnt

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

//output

('Numbers read from file numbers.txt is: ', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
Add a comment
Know the answer?
Add Answer to:
Question 2: Assume that a text file containing a series of integers is named numbers.txt and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT