I have made a table where the user could input his/her own values.
The program is to work as shown in the picture.
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.
@518nad
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.
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()
you can have list of lists so
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]]
Yea but the User would have to specify how many rows and columns there will be. So I can't make a fixed list.
Is there a way I could make my code display that outcome as a string instead of a list?
without using .join, I haven't learned that yet.
okay here is a way to initialize a set of row and col
?
R=3 C=3 A=[] for i in range(R): A.append([]) for j in range(C) A[i].append(0)
u can make R and C userinput
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)
then for gathering data
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
Hm I've never seen it like that before Where you have the name of your list than an index before the append.
it means to append it to that part of the list index
uve learnt about indexing through lists right
do they want u to print in table form btw
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]
use that if u want to printe in table form or the other one if u just want A displayed
Yea I have, i just never seen it before append before like that. lol
welll i dunno what to say, its normal
do u understand wat its doing
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.
what do u mean its not vertical
Gimme a sec
okay i see ur code its looks fine,
what do u want the print to look like
a bit confused rn
Sorry. Gimme a sec.
okay
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.
u can join each value of a list to another value with a bunch of spaces inbetween and print
using .join? I haven't learned that yet. So i don't think my professor would approve of it.
no dont use join
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
hey u there?
oh actually i dont think that allings it nicely if u got bigger numbers right
u can use a formula for allignment
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
r u there?! u always leave -.-
you can even make this like a robust table, by account for max string length
accounting
for example u can use maxstring length variable instead 10, so just like add 3 spaces or something extra to max string length
Sorry Sorry, was occupied by stuff.
row = row + str(A[i][j]) +(10-len(str(A[i][j])))*' ' What does that do? That looks super confusing xD
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.
Join our real-time social learning platform and learn together with your friends!