5 Replies - 1405 Views - Last Post: 22 November 2012 - 05:12 PM Rate Topic: -----

#1 Khantan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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. :sorcerer:/>/>

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  Icon User is online

  • D.I.C Lover

Reputation: 198
  • Posts: 1,688
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Khantan  Icon User is offline

  • New D.I.C Head

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

Re: Populate Matrix with Strings

Posted 22 November 2012 - 03:56 PM

Thanks for your fast answer.

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 :)
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 Khantan  Icon User is offline

  • New D.I.C Head

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

Re: Populate Matrix with Strings

Posted 22 November 2012 - 05:08 PM

Fixed the problem in another forum :bananaman:


After reading n, i inserted
a = [ [ '~' for _ in range(n) ] for _ in range(n) ]


now works.

Halleluyah

View PostSimown, 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!! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1