r/pythonhelp • • Oct 24 '19

Python Questions

I need help with this 3 python questions.

please I just need the answers no explanation please here they are:

  1. A file named data1.txt contains an unknown number of lines, each consisting of a single integer. Write some code that creates a file named data2.txt and copies all the lines of data1.txt to data2.txt.

  2. A file named numbers.txt contains an unknown number of lines, each consisting of a single positive integer. Write some code that reads through the file, ignoring those value that are not bigger than the maximum value read up to that point. The numbers that are NOT ignored are added, and their sum stored in a variable called runsum.

For example, if the sequence of integers in the file were "9 7 5 18 13 2 22 16" the code would ignore: 7, 5, 13, 2 and 16; it would add 9, 18, and 22, storing their sum, 49, into runsum.

3. Two files named numbers1.txt and numbers2.txt both have an unknown number of lines, each line consisting of a single positive integer. Write some code that reads a line from one file and then a line from the other file. The two integers are multiplied together and their product is added to a variable called scalar_product which should be initialized to zero.

Your code should stop when it detects end of file in either file that it is reading.

For example, if the sequence of integers in one file was "9 7 5 18 13 2 22 16" and "4 7 8 2" in the other file, your code would compute:

49 + 77 + 85 + 218

and thus store 161 into scalar_product.

0 Upvotes

6 comments sorted by

View all comments

2

u/[deleted] Oct 24 '19

We aren’t here to do you homework for you. This is very basic stuff. If you can’t do this, best to drop the class because it’s only going to get harder and you won’t be able to do it. Sorry to be the bearer of bad news.