Reputation: 147
I need this function to output to a .txt file, with what I have below it is returning only last row of integers.
def random_grid(file):
grid = []
num_rows = raw_input("How many raws would you like in your grid? ")
num_columns = raw_input("How many columns would you like in your grid? ")
min_range = raw_input("What is the minimum number you would like in your grid? ")
max_range = raw_input("what is the maximum number you would like in your grid? ")
for row in range(int(num_rows)):
grid.append([])
for column in range(int(num_columns)):
grid[row].append(random.randint((int(min_range)),(int(max_range))))
for row in grid:
x = (' '.join([str(x) for x in row]))
print x
with open(r"test.txt", 'w') as text_file:
text_file.write(x)
If the user choose a 3 by 3 grid, a low number of 1 and high number of 9 it could print like this.
1 2 3
4 5 6
7 8 9
I am only getting
7 8 9
in my outputted .txt file
Upvotes: 0
Views: 279
Reputation: 191710
You need to append to the file instead of overwrite it
Change
with open(r"test.txt", 'w') as text_file:
to
with open(r"test.txt", 'a') as text_file:
for append mode
or move the with open(r"test.txt", 'w') as text_file:
to above the for row in grid
loop
import random
def random_grid(file):
grid = []
num_rows = int(raw_input("How many rows would you like in your grid? "))
num_columns = int(raw_input("How many columns would you like in your grid? "))
min_range = int(raw_input("What is the minimum number you would like in your grid? "))
max_range = int(raw_input("What is the maximum number you would like in your grid? "))
for _ in range(num_rows):
grid.append([random.randint(min_range,max_range) for _ in range(num_columns)])
grid_str = '\n'.join(' '.join(map(str, row)) for row in grid)
with open(r"test.txt", 'w') as text_file:
text_file.write(grid_str)
Upvotes: 3
Reputation: 11807
The second option of open
, w
, will overwrite the file's contents. Use a
to append instead, but also put a newline character \n
after each line.
def random_grid(file):
grid = []
num_rows = raw_input("How many raws would you like in your grid? ")
num_columns = raw_input("How many columns would you like in your grid? ")
min_range = raw_input("What is the minimum number you would like in your grid? ")
max_range = raw_input("what is the maximum number you would like in your grid? ")
for row in range(int(num_rows)):
grid.append([])
for column in range(int(num_columns)):
grid[row].append(random.randint((int(min_range)),(int(max_range))))
for row in grid:
x = (' '.join([str(x) for x in row]))
print x
with open(r"test.txt", 'a') as text_file:
text_file.write(x)
text_file.write("\n")
The other, more efficient way to do it is to move your file-writing code outside the loop, like this:
def random_grid(file):
grid = []
num_rows = raw_input("How many raws would you like in your grid? ")
num_columns = raw_input("How many columns would you like in your grid? ")
min_range = raw_input("What is the minimum number you would like in your grid? ")
max_range = raw_input("what is the maximum number you would like in your grid? ")
for row in range(int(num_rows)):
grid.append([])
for column in range(int(num_columns)):
grid[row].append(random.randint((int(min_range)),(int(max_range))))
x = ""
for row in grid:
x += (' '.join([str(x) for x in row])) + "\n"
print x
with open(r"test.txt", 'w') as text_file:
text_file.write(x)
Upvotes: 3