[Python] I need to make a code that presents the given results.
So the user would input the number of rows and columns. Then the user would input which number go in the rows and columns. The the given input would be displayed as a table as shown in the picture.
This is what I have so far.... Here is my current code: def main(): rowsList =[] colsList = [] 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: rowNum = int(input("Enter the Value (1): ")) except ValueError: print("Invalid Input") continue else: break rowsList.append(int(rowNum)) for i in range (2,row+1): rowNum = int(input("Enter the Value (" + str(i) + "): ")) rowsList.append(int(rowNum)) print(rowsList) 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 while True: try: colNum = int(input("Enter the Value (1): ")) except ValueError: print("Invalid Input") continue else: break colsList.append(int(colNum)) for i in range (2,col+1): colNum = int(input("Enter the Value (" + str(i) + "): ")) colsList.append(int(colNum)) print(colsList) Table(rowsList,colsList) def Table(rowsList, colsList): print(rowsList, colsList) main()
@518nad @SapphireMoon
Sorry, I could do it in Java but not good with Python arrays.
You set up two one dimensional arrays. You want one two dimensional array. The syntax for working with a 2D array is: array[][] If you have 3 rows of 4 columns you could create it with: ``` >>> for rows in xrange( 3 ): ... foo.append( [] ) ... for cols in xrange( 4 ): ... foo[ rows ].append( 0 ) ... >>> foo [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] ``` Lists are zero indexed ( rows will be 0-2 ).
Sorry i am a little lost. Could you elaborate?
The User tells the program how many rows and columns he/she wants. Then what numbers go in each List. Each list is then displayed vertically as part of the table. Like its shown in the picture
A 2 dimensional array is actually an array of arrays. A matrix, which is what your picture looks like, can be thought of as an array of rows, each of which is an array of columns. Since there are two dimensions, two indexes are needed to address an individual item. To populate a two dimensional array, you generally use a nested loop.
hi
hi
Okay but then how would I display the numbers as a table? And the numbers inputted for each list is Vertical in the table...
Display is a matter of formatted printing.
Okay I edited my code a bit.
Here is my new Code: 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) def Table(rowsList): for nums in rowsList: print(nums) main()
Now I need it to print each column vertically side by side. How would I format it to do that?
``` def draw(nRows, nCols): vals = [] for i in range(nCols): temp = [] for j in range(nRows): temp.append(input('Entry: ')) vals.append(temp) return '\n'.join([ '|'.join([x for x in val]) for val in zip(*vals) ]) print(draw(3, 3)) ``` Output: ``` Entry: 1 Entry: 2 Entry: 3 Entry: 4 Entry: 5 Entry: 6 Entry: 7 Entry: 8 Entry: 9 1|4|7 2|5|8 3|6|9 ```
I didnt learn zip or .join yet. Is there another simpler way?
@Agent47 is writing production Python code, beyond the scope of your course. You can use nested loops to go through rowsList and columsList to get each value.
Join our real-time social learning platform and learn together with your friends!