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/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!

1

u/SleepyHollow100 Oct 27 '19

Am in College I dont know anything about coding I dont like coding am taking this class because my major requires it and the language just sounds greek to me I dont really get your explanation am sure is helpful but Idk anything about coding I understand I have to learn on my own but is just that python doesn't make any sense to me

1

u/SleepyHollow100 Oct 27 '19

I never took this class in High School

1

u/[deleted] Jul 28 '23

Idk if you'll see this cause its been so long, but don't worry bro I understand you. I'm currently taking coding and what I've learned is simply its not as hard as it looks, nor does it take a genius to figure it out. but it takes time and passion. If you don't have passion or time to do it like anything it wont make sense. don't worry about what people say, there is this illusion that coders are Ivy league level geniuses and its not the case its really just a bunch of people googling answers and learning as they gain more experience.