Working with files filling up with numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Working with files filling up with numbers

WoNT WORK DESCRIPTION: Take a number N as input and write the numbers 1 to N to the file "numbers.txt", each number on a separate line. Sample Input: 4 Sample Output: 1 2 3 4 The given code reads the content of the file and outputs it.

17th Feb 2021, 7:11 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
5 Answers
+ 10
n = int(input()) file = open("numbers.txt", "w+") #your code goes here for line in range(1, n+1): file.write(str(line)+"\n") file.close() f = open("numbers.txt", "r") print(f.read()) f.close() This is also works ....
23rd May 2021, 5:33 AM
Abhishek Biswas
Abhishek Biswas - avatar
+ 2
This is my answer. n = int(input()) i = 1 file = open("numbers.txt", "w+") #your code goes here while i <= n: file.write(str(i) + "\n") i += 1 file.close() f = open("numbers.txt", "r") print(f.read()) f.close()
3rd Apr 2021, 6:44 AM
Gunjan Patel
Gunjan Patel - avatar
0
n = int(input()) file = open("numbers.txt", "w+") #your code goes here for line in range(1, n+1): file.write(str(line)+"\n") file.close() f = open("numbers.txt", "r") print(f.read()) f.close()
10th Nov 2022, 8:48 AM
Pooja Patel
Pooja Patel - avatar
0
n = int(input()) file = open("numbers.txt", "w+") #my code is here for i in range(n): file.write(str(i+1)+"\n") print("\n") file.close() f = open("numbers.txt", "r") print(f.read()) f.close()
2nd Dec 2023, 10:37 AM
vahid kamrani
vahid kamrani - avatar
- 1
not sure how to complete it n = int(input()) file = open("numbers.txt", "w+") #your code goes here f = open("numbers.txt", "r") print(f.read()) f.close()
17th Feb 2021, 7:12 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar