Wilgens Metelus
Wilgens Metelus

Reputation: 25

Creating a file with a list

so for my code, I am trying to create a program that creates a file, and enters this into the file.

0 1
1 2
2 3
3 4
4 5

I was trying to get a for loop to obtain this, but I am getting an error with using an int in a string. How would you tell the program to also print the first number in the row plus one without using the +1 in the write?

f = open("data.txt","w")
for int in range(0,4):
    f.write(int)
    f.write(int+1)
    f.write("\n")

Upvotes: 0

Views: 703

Answers (2)

Nikolaj Kiel
Nikolaj Kiel

Reputation: 86

Use str(i) or:

with open('data.txt','w') as fout:
    for i in range(0,5):
        fout.write(f'{i} {i+1}\n')

Upvotes: 0

f = open("data.txt","w")
for i in range (0,5):
    f.write(str(i))
    f.write(str(i+1))
    f.write("\n")
f.close()

Upvotes: 1

Related Questions