Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (itz_sid):

I have made a table where the user could input his/her own values.

OpenStudy (itz_sid):

The program is to work as shown in the picture.

OpenStudy (itz_sid):

I was able to make the program. However, my program displays the values in a list, unaligned. Is there a way I could make the numbers display as a string and perfectly aligned? I have not learned .join yet. So if there is another way, even if it may be more complicated, please show me.

OpenStudy (itz_sid):

@518nad

OpenStudy (itz_sid):

Using Python. The initial goal is to create a program where the user inputs the number of rows and columns. Then the user would input the values for each row. Then through the given input of values, the program would create a table.

OpenStudy (itz_sid):

Here is my current code: (If anyone wants to tinker with it) def main(): rowsList =[] while True: try: row = int(input("Please Enter the Number of Rows: ")) while row < 0 : print("Invalid Input (No Negative Numbers)") row = int(input("Please Enter the Number of Rows: ")) except ValueError: print("Invalid Input") continue else: break while True: try: col = int(input("Please Enter the Number of Columns: ")) while col < 0 : print("Invalid Input (No Negative Numbers)") col = int(input("Please Enter the Number of Columns: ")) except ValueError: print("Invalid Input") continue else: break print() for i in range(1,col+1): print("[Column " + str(i)+ "]") for x in range (1,row+1): rowNum = int(input("Enter the Value (Row " + str(x) + "): ")) rowsList.append(int(rowNum)) print() Table(rowsList,row) def Table(rowsList,row): for num in range (0,row): print(str(rowsList[int(str(num)):len(rowsList):row])) main()

OpenStudy (518nad):

you can have list of lists so

OpenStudy (518nad):

for example, this is like having 3 rows A=[ [ ],[ ],[ ] ] and 3 colums and 3 rows is this A=[ [0,0,0],[0,0,0],[0,0,0]]

OpenStudy (itz_sid):

Yea but the User would have to specify how many rows and columns there will be. So I can't make a fixed list.

OpenStudy (itz_sid):

Is there a way I could make my code display that outcome as a string instead of a list?

OpenStudy (itz_sid):

without using .join, I haven't learned that yet.

OpenStudy (518nad):

okay here is a way to initialize a set of row and col

OpenStudy (itz_sid):

?

OpenStudy (518nad):

R=3 C=3 A=[] for i in range(R): A.append([]) for j in range(C) A[i].append(0)

OpenStudy (518nad):

u can make R and C userinput

OpenStudy (518nad):

R=int(input('Rows:= ')) C=int(input('col:= ')) A=[] for i in range(R): A.append([]) for j in range(C) A[i].append(0)

OpenStudy (518nad):

then for gathering data

OpenStudy (518nad):

R=int(input('Rows:= ')) C=int(input('col:= ')) A=[] for i in range(R): A.append([]) for j in range(C): A[i].append(int(input('enter value of ' + str(i+1)+',' + str(j+1)+' :'))) print A

OpenStudy (itz_sid):

Hm I've never seen it like that before Where you have the name of your list than an index before the append.

OpenStudy (518nad):

it means to append it to that part of the list index

OpenStudy (518nad):

uve learnt about indexing through lists right

OpenStudy (518nad):

do they want u to print in table form btw

OpenStudy (518nad):

R=int(input('Rows:= ')) C=int(input('col:= ')) A=[] for i in range(R): A.append([]) for j in range(C): A[i].append(int(input('enter value of ' + str(i+1)+',' + str(j+1)+' :'))) for i in range(R): print A[i]

OpenStudy (518nad):

use that if u want to printe in table form or the other one if u just want A displayed

OpenStudy (itz_sid):

Yea I have, i just never seen it before append before like that. lol

OpenStudy (518nad):

welll i dunno what to say, its normal

OpenStudy (518nad):

do u understand wat its doing

OpenStudy (itz_sid):

I mean its doing pretty much the same thing that my code was doing. It is displaying the table like a list. But yours isnt displaying the given numbers vertically as shown in the picture for the table.

OpenStudy (518nad):

what do u mean its not vertical

OpenStudy (itz_sid):

Gimme a sec

OpenStudy (518nad):

okay i see ur code its looks fine,

OpenStudy (518nad):

what do u want the print to look like

OpenStudy (518nad):

a bit confused rn

OpenStudy (itz_sid):

Sorry. Gimme a sec.

OpenStudy (518nad):

okay

OpenStudy (itz_sid):

Like this other lab prints a multiplication table. Its doesnt have the brackets and commas and is perfectly aligned. I wanted it to be like that.

OpenStudy (518nad):

u can join each value of a list to another value with a bunch of spaces inbetween and print

OpenStudy (itz_sid):

using .join? I haven't learned that yet. So i don't think my professor would approve of it.

OpenStudy (518nad):

no dont use join

OpenStudy (518nad):

R=int(input('Rows:= ')) C=int(input('col:= ')) A=[] for i in range(R): A.append([]) for j in range(C): A[i].append(int(input('enter value of ' + str(i+1)+',' + str(j+1)+' :'))) for i in range(R): print A[i] for i in range(len(A)): row='' for j in range(len(A[i])): row = row + str(A[i][j]) +' ' print row

OpenStudy (518nad):

hey u there?

OpenStudy (518nad):

oh actually i dont think that allings it nicely if u got bigger numbers right

OpenStudy (518nad):

u can use a formula for allignment

OpenStudy (518nad):

like so R=int(input('Rows:= ')) C=int(input('col:= ')) A=[] for i in range(R): A.append([]) for j in range(C): A[i].append(int(input('enter value of ' + str(i+1)+',' + str(j+1)+' :'))) for i in range(R): print A[i] for i in range(len(A)): row='' for j in range(len(A[i])): row = row + str(A[i][j]) +(10-len(str(A[i][j])))*' ' print row

OpenStudy (518nad):

r u there?! u always leave -.-

OpenStudy (518nad):

you can even make this like a robust table, by account for max string length

OpenStudy (518nad):

accounting

OpenStudy (518nad):

for example u can use maxstring length variable instead 10, so just like add 3 spaces or something extra to max string length

OpenStudy (itz_sid):

Sorry Sorry, was occupied by stuff.

OpenStudy (itz_sid):

row = row + str(A[i][j]) +(10-len(str(A[i][j])))*' ' What does that do? That looks super confusing xD

OpenStudy (rsmith6559):

Space it out so that it's more readable. It pads str( A[ i ][ j ] ) with spaces to be 10 characters long. It's a good piece of code to understand and play with. It's also a lesson in commenting nifty bits of code.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!