1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ne4ueva [31]
3 years ago
14

A file named data.txt contains an unknown number of lines, each consisting of a single integer. Write some code that creates two

files, dataplus.txt and dataminus.txt, and copies all the lines of data1.txt that have positive integers to dataplus.txt, and all the lines of data1.txt that have negative integers to dataminus.txt. Zeros are not copied anywhere.
Computers and Technology
2 answers:
slavikrds [6]3 years ago
6 0

Answer:

#section 1

data = open('data.txt', 'r')

num = (data.read())

num = num.split()

data.close()

#section 2  

for x in num:

   x = int(x)

   if x < 0:

       minus = open('dataminus.txt', 'a')

       minus.write(str(x) + ' ')

       minus.close()

   elif x > 0:

       plus = open('dataplus.txt', 'a')

       plus.write(str(x)+' ')

       plus.close()

Explanation:

#section 1

A file data.txt containing an unknown number of lines each containing a single integer is opened.

The data is read from it and split to form a list from which the code will iterate over with a FOR loop and some conditional statements will be used to determine if the number is positive or negative.

#section 2

In this section two new files are created/ opened dataplus.txt and dataminus.txt and all the positive integers are stored in the dataplus.txt and the negative integers in the dataminus.txt.

The IF statement and elif statement used ensures that zero(0) is not captured ensuring that the code fulfills all the requirements in your question.

Damm [24]3 years ago
3 0

Answer:

I am doing it with python.

Explanation:

nums = '9 -8 -7 -6 -5 -4 -2 0 1 5 9 6 7 4'

myfile = open('data.txt', 'w')

myfile.write(nums)

myfile.close()

myfile = open('data.txt', 'r')

num1 = (myfile.read())

num1 = num1.split()

print(num1)

print(type(num1))

for x in num1:

   x = int(x)

   if x < 0:

       minus = open('dataminus.txt', 'a')

       minus.write(str(x) + ' ')

       minus.close()

   elif x>= 0:

       plus = open('dataplus.txt', 'a')

       plus.write(str(x)+' ')

       plus.close()

You might be interested in
Which is the correct sequence of steps for opening a new document?
ollegr [7]
Basic steps start with opening the program, clicking on File, then clicking on New for a new document.

Most programs open a new blank document when you open he program.
6 0
2 years ago
Read 2 more answers
Hi Im really a girl and i want to know how to change my username without having to make a new account?
bixtya [17]
No sadly.
You can change your e-mail, profile picture, but that's about it.
I didn't see an option to change your name.

7 0
3 years ago
Read 2 more answers
Derek found that the CPU was running several processes. While Derek was looking at Task Manager, the computer crashed. Derek res
wlad13 [49]

Answer: d. Run a virus scan

Explanation: Running a virus scan will reveal if the trojan virus or any other malware has been launched and the clearing the virus when detected. These will make the computer to start working well again. The unexplainable processes that started when the computer was rebooted can be viruses.

8 0
3 years ago
Read 2 more answers
HELP ASAP!!!
stiks02 [169]
Comparison would be most suitable. I hope I could be a help


7 0
3 years ago
Read 2 more answers
Can I have help on this
never [62]

the answer is the seconf one


6 0
3 years ago
Read 2 more answers
Other questions:
  • Java - Given a String variable response that has already been declared, write some code that repeatedly reads a value from stand
    12·1 answer
  • 1.
    13·1 answer
  • What is a break in the content that forces takes on to the next line
    10·2 answers
  • You use Cat5e twisted pair cable on your network. Cables are routed through walls and the ceiling. A user puts a screw in the wa
    9·1 answer
  • State whether true or false.
    6·1 answer
  • Which operating system is a version of Linux?​
    13·1 answer
  • What are the correct answers?
    11·1 answer
  • Cuáles eran las condiciones de vida de las mujeres durante el renacimiento perduran​
    14·1 answer
  • ¿por que hay peligros en internet?
    11·1 answer
  • 12. Why is it so important to pay attention to your digital reputation?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!