r/pythonhelp • u/SleepyHollow100 • 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:
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.
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.
2
u/xelf Oct 25 '19
prob#1 : just write to the second file as you read the first one.
prob#2: Use < and > to compare numbers, use + to add them, for instance 9+18 will give you 27. And 9 is > 7
prob#3: again here you can use + to add two numbers, and you can use * to multiply two numbers.
Good luck with your homework, if you don't mind me asking, what grade/year is this for? My son is taking intro to python in 8th grade, and he is doing way more complicated stuff than this after the 3rd week.
Let me know if you need more help, good luck to you!