I am trying to create a board that lets users choose the number of rows and columns but I can’t seem to put anything together!
For example if a user wants 3 rows and 3 columns I am trying to make it print out:
xxx xxx xxx
My code so far:
def main() #ask for rows: print("Please choose the num of rows:") rows = int(input("Please enter a number: ")) #ask for columns: print("Please choose num of columns:") columns = int(input("Please enter a number: ")) #print rows: board = [] for r in range(0, rows - 1): r = "x" print(r) #print columns: for k in range(0, columns): board = board + ["x"] print(board) main()
and this is what it outputs:
Please choose the number of rows: Please enter a number: 3 Please choose the number of columns: Please enter a number: 3 x x ['x', 'x', 'x']
I feel really dumb for not being able to do something as basic as this but this was the best I was able to come up with! I sort of got it to print the number of rows / columns but i have no idea how to make it look “normal” or fill it in..
Can anyone more knowledgeable help me?
Thank you!
Advertisement
Answer
One of the many ways to do it:
for x in range(row): print('x '*col)
For example, row =3
, col =3
then output :
x x x x x x x x x
Alternatively,
Create a list-
a=[] for x in range(0, row): a.append(["x"] * col)
Then print it-
for row in a: print " ".join(row)