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

[Python] I need to make a code that presents the given results.

OpenStudy (itz_sid):

OpenStudy (itz_sid):

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.

OpenStudy (itz_sid):

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()

OpenStudy (itz_sid):

@518nad @SapphireMoon

OpenStudy (sapphiremoon):

Sorry, I could do it in Java but not good with Python arrays.

OpenStudy (rsmith6559):

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 ).

OpenStudy (itz_sid):

Sorry i am a little lost. Could you elaborate?

OpenStudy (itz_sid):

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

OpenStudy (rsmith6559):

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.

OpenStudy (joshmorris):

hi

OpenStudy (bonnieisflash1.0):

hi

OpenStudy (itz_sid):

Okay but then how would I display the numbers as a table? And the numbers inputted for each list is Vertical in the table...

OpenStudy (rsmith6559):

Display is a matter of formatted printing.

OpenStudy (itz_sid):

Okay I edited my code a bit.

OpenStudy (itz_sid):

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()

OpenStudy (itz_sid):

Now I need it to print each column vertically side by side. How would I format it to do that?

OpenStudy (agent47):

``` 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 ```

OpenStudy (itz_sid):

I didnt learn zip or .join yet. Is there another simpler way?

OpenStudy (rsmith6559):

@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.

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!
Latest Questions
DonaldTrumpofQC: Happy St. Patty's Day
6 hours ago 3 Replies 0 Medals
gelphielvr: (BIOLOGY 1) Carbon & Nitrogen (question in the replies)
12 hours ago 1 Reply 0 Medals
jmweeden: how do you draw realistically?
14 hours ago 0 Replies 0 Medals
jmweeden: if you guys had 1 million$ would you spend or save? why?
14 hours ago 0 Replies 0 Medals
jmweeden: how do you jump rope?
14 hours ago 6 Replies 0 Medals
Shrade3: Chemisty please, annoying
23 hours ago 4 Replies 0 Medals
girlypop: how do I save money more?
15 hours ago 18 Replies 4 Medals
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!