# Populate Matrix with Strings

Page 1 of 1

## 5 Replies - 2503 Views - Last Post: 22 November 2012 - 05:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=301237&amp;s=ba0c72078ff93d7c469052b31cae9e5b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Khantan

Reputation: 0
• Posts: 3
• Joined: 22-November 12

# Populate Matrix with Strings

Posted 22 November 2012 - 03:25 PM

English is not my strong language, please considerate it.

What I'm trying to do is make a 3x3, 4x4 or 5x5 matrix of "~" signs, that would get replaced by "X" 3 times,

depending on coordinates given by x and y imputs, appended in s=[]

so for example if i have a matrix 3x3
~~~
~~~
~~~

and coordinate (0,0),

the result should be:
X~~
~~~
~~~

As easy as it sounds it's not easy for me still, I get problem even if my code seems logical to me.

Some help would be so much appreciated, thanks. />/>

```a = []

n=0
while n<3 or n>5:
n=int(raw_input("type matrix: "))
for i in range (n):
for j in range (n):
print "~",
print "\n",
def zdruzi(a):
for row in a:
print " ".join(row)
zdruzi(a)

s = []
for i in range(3):
x=int(raw_input("x: "))
y=int(raw_input("y: "))
s.append(int(x))
s.append(int(y))
a[int(x)][int(y)]="X"

if (x<0) or (x>(n-1)):
print "not good"
break

print a

```

This post has been edited by macosxnerd101: 22 November 2012 - 04:26 PM
Reason for edit:: Please use a descriptive title

Is This A Good Question/Topic? 0

## Replies To: Populate Matrix with Strings

### #2 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,717
• Joined: 13-March 10

## Re: Populate Matrix with Strings

Posted 22 November 2012 - 03:54 PM

Once you cast x and y to int(), they are integers, so no need to do it again.

### #3 Khantan

Reputation: 0
• Posts: 3
• Joined: 22-November 12

## Re: Populate Matrix with Strings

Posted 22 November 2012 - 03:56 PM

But I'm afraid this doesn't solve my problem.

The problem is

IndexError: list index out of range

If that helps you find my problem

### #4 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Populate Matrix with Strings

Posted 22 November 2012 - 05:06 PM

Hello! I had a look at your program and this seems to be the problem:

When you are trying to create the matrix you are not actually populating a at all! It will be empty!

You:
```print "~"
```

But you mean to set :
```a[i][j] = "~"

```

Do you understand what I mean? Also, a is not a 2 dimensional array, it's a 1 dimensional structure at the moment. If you need more help fixing it we'd be happy to help.

### #5 Khantan

Reputation: 0
• Posts: 3
• Joined: 22-November 12

## Re: Populate Matrix with Strings

Posted 22 November 2012 - 05:08 PM

Fixed the problem in another forum

```a = [ [ '~' for _ in range(n) ] for _ in range(n) ]
```

now works.

Halleluyah

Simown, on 22 November 2012 - 05:06 PM, said:

Hello! I had a look at your program and this seems to be the problem:

When you are trying to create the matrix you are not actually populating a at all! It will be empty!

You:
```print "~"
```

But you mean to set :
```a[i][j] = "~"

```

Do you understand what I mean? Also, a is not a 2 dimensional array, it's a 1 dimensional structure at the moment. If you need more help fixing it we'd be happy to help.

Didn't see your post, you must've posted a few seconds before me.

It's so simple now, that I know the answer, I'm almost ashamed now. Thank you anyway!!

### #6 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Populate Matrix with Strings

Posted 22 November 2012 - 05:12 PM

Yes. Maybe I was too slow posting Glad it works now!