0

I'm working on an assignment in which I need to print a "diamond" after the user typed in the dimensions they wanted. Example:

If the user types 5 it looks like this:

  |
 |||
|||||
 |||
  |

If the user types 10 it looks like this:

      |
     |||
    |||||
   |||||||
  |||||||||
  |||||||||
   |||||||
    |||||
     |||
      | 

My idea is to initialize an array for each row that I need to print but I don't know how to automatically initialize multiple arrays corresponding to the number the user typed in.

Any help is welcome and feel free to tell me if you think I'm going totally the wrong way!

2
  • row = [ "some_char" ] * 10 Commented Jan 26, 2016 at 19:51
  • get the user input dimensions and then do a for loop and print arrays depending on the input Commented Jan 26, 2016 at 19:57

1 Answer 1

1

If it helps you can use "array" (or rather two-dimensional list).

To create row with 10 letters "X"

row = [ "X" ] * 10

But to create "array" with 10 rows you can't do

array = [  [ "X" ] * 10  ] * 10

because you get all rows as references to the same space in memory.

You need

array = []

for i in range(10)
    row = [ "X" ] * 10
    array.append(row)

Or shorter

array = [ [ "X" ] * 10 for i in range(10) ]

(in place of "X" you can use space " ")

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It does help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.