# Why can't I do this multiplication?

• (2 Pages)
• 1
• 2

## 16 Replies - 6846 Views - Last Post: 08 October 2011 - 11:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=250198&amp;s=7a9fd679f24bddfb2c2059ba84c1cfd2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pvpkiller

Reputation: 0
• Posts: 33
• Joined: 24-June 11

# Why can't I do this multiplication?

Posted 06 October 2011 - 05:37 PM

```def set_a(v11, v12, v13, v21, v22, v23, v31, v32, v33):
global a11, a12, a13, a21, a22, a23, a31, a32, a33
a11, a12, a13 = v11, v12, v13
a21, a22, a23 = v21, v22, v23
a31, a32, a33 = v31, v32, v33

def multiply_a_by_scalar(value):

#Multiplies each vector by a value specified by the user.
a11 = v11 * value
a12 = v12 * value
a13 = v13 * value
a21 = v21 * value
a22 = v22 * value
a23 = v23 * value
a31 = v31 * value
a32 = v32 * value
a33 = v33 * value

return a11, a12, a13, a21, a22, a23, a31, a32, a33

```

I dont want to change the function set_a. But i get an error saying v11 is not defined. So how do i fix that? Please dont teach me easier ways such as lists and stuff. I am trying to do this the basic way, as i am very new to python. Any help is appreciated.

Is This A Good Question/Topic? 0

## Replies To: Why can't I do this multiplication?

### #2 Simown

• Blue Sprat

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

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 06:16 PM

v11 along with v12, v13, v21, v22, v23, v31, v32 and v33 are only in scope in the set_a function where they are passed as parameters, so it doesn't theoretically exist in the other function. Global a11 takes the value of v11 in this function, so we can use that.

```def multiply_by_a_scalar(value):
a11 = a11 * value
a12 = a12 * value
....
....

```

All this is multiplying the global value of a11 by some value and storing it back in a11. Same for the other variables.

This post has been edited by Simown: 06 October 2011 - 06:21 PM

### #3 pvpkiller

Reputation: 0
• Posts: 33
• Joined: 24-June 11

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 06:25 PM

Thank you, but that doesn't work. That brings a new error "UnboundLocalError: local variable 'a11' referenced before assignment." Any other way?

### #4 Simown

• Blue Sprat

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

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 06:33 PM

Oh, sorry, you are using global variables.

You need to include the:
```global a11, a12, a13, a21, a22, a23, a31, a32, a33
```

Inside the multiply_a_by_scalar function. Should fix it

This post has been edited by Simown: 06 October 2011 - 06:33 PM

### #5 pvpkiller

Reputation: 0
• Posts: 33
• Joined: 24-June 11

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 06:38 PM

Omg, ur the best! It works now. THANK YOU SO MUCH!!!

### #6 Simown

• Blue Sprat

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

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 06:43 PM

My pleasure, always happy to help

This post has been edited by Simown: 06 October 2011 - 06:44 PM

### #7 pvpkiller

Reputation: 0
• Posts: 33
• Joined: 24-June 11

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 06:53 PM

just to clarify. Lets say i have a random function, and inside this function, i create variables. Now if i place "global" before the variables, then i can use that variable in every other function i make?

### #8 Simown

• Blue Sprat

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

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 07:12 PM

You can do it that way, yes. Write global before the variable names in the function you are using them in, and then assign to them afterwards, much like you did in the two previous functions.

I would try and steer you away from global variables, it leads to confusing code, as the variables are non-local - variables can be modified by any part of the program, often making it hard to keep track of the use and modification of them, especially as the program size grows.

If this is the way you are learning now, keep at it. But remember that using local variables over global is a better programming practice.

This post has been edited by Simown: 06 October 2011 - 07:17 PM

### #9 Martyr2

• Programming Theoretician

Reputation: 5207
• Posts: 13,955
• Joined: 18-April 07

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 08:05 PM

pvpkiller, I thought we had this licked? Why are you throwing away the previous solution?

I gave you the entire thing. Why are you wasting Simown's time?

### #10 pvpkiller

Reputation: 0
• Posts: 33
• Joined: 24-June 11

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 08:09 PM

I understand what you are saying. If u have time, (only if u have time) would u mind showing me how i would use local variables to accomplish what i wanted with the previous functions? U are very helpful, i really appreciate it

Martyr2, on 06 October 2011 - 08:05 PM, said:

pvpkiller, I thought we had this licked? Why are you throwing away the previous solution?

I gave you the entire thing. Why are you wasting Simown's time?

not being rude or anything, buy Simown helped me a lot more, cause i am very new to python and he did it the way i wanted it done. You are also very helpful, but ur way is very complicated and i still didn't understand what u said last time. Its just ur explanation is good for someone who isn't very new to python, thats all. But still, thank u.

### #11 Martyr2

• Programming Theoretician

Reputation: 5207
• Posts: 13,955
• Joined: 18-April 07

## Re: Why can't I do this multiplication?

Posted 06 October 2011 - 09:54 PM

Ok, well that is why then you say "I don't understand _____". I am trying to also make you a better programmer. The code you are writing is horrendous and I just wanted you to avoid problems I see you will be making very shortly.

But it is fine, if you don't want to follow the approach I put out that is ok. Just know that you are heading down a dangerous path and I hope things work out for you.

### #12 Simown

• Blue Sprat

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

## Re: Why can't I do this multiplication?

Posted 07 October 2011 - 04:22 AM

I'd have to agree with Martyr2, the code you have now, that you say you don't want to change, isn't the best it could be.

I'll write your code using local variables and explain breifly why it's a better solution (I'll try and keep most of it the same):

```def multiply_a_by_scalar(v11, v12, v13, v21, v22, v23, v31, v32, v33, value):

#Multiplies each vector by a value specified by the user.
v11 = v11 * value
v12 = v12 * value
v13 = v13 * value
v21 = v21 * value
v22 = v22 * value
v23 = v23 * value
v31 = v31 * value
v32 = v32 * value
v33 = v33 * value
return v11, v12, v13, v21, v22, v23, v31, v32, v33

```

Now it doesn't use global variables at all, the "v" variables are local to the function, I have removed the set_a function because it was unnecessary just to set global variables. This isn't to say you can't reuse the results of this function somewhere else:

```>>> multiply_a_by_scalar(1, 2, 3, 4, 5, 6, 7, 8, 9, value = 1)
(1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> multiply_a_by_scalar(1, 2, 3, 4, 5, 6, 7, 8, 9, value = 2)
(2, 4, 6, 8, 10, 12, 14, 16, 18)
>>> threeTimes = multiply_a_by_scalar(1, 1, 1, 2, 2, 2, 3, 3, 3, value = 3)
>>> threeTimes
(3, 3, 3, 6, 6, 6, 9, 9, 9)

```

It has exactly the same functionality but doesn't use globals, and doesn't have dependence on the other function. This way the entire functionality of the function is enclosed. If you wanted to change how the function works, then you only need to modify this one bit of code.

Hope that makes it a bit clearer

### #13 baavgai

• Dreaming Coder

Reputation: 7164
• Posts: 14,931
• Joined: 16-October 07

## Re: Why can't I do this multiplication?

Posted 07 October 2011 - 05:53 AM

I'm sorry, I can't take it anymore. Use a list, man! Or an object, or something. Loops are your friend.

Globals are bad. You're doing ok now, but what about when I ask you to multiply two matrices? Globals stop you from that. Here's a simple example of using lists:

```def get_matrix(v11, v12, v13, v21, v22, v23, v31, v32, v33):
return [[v11, v12, v13], [v21, v22, v23], [v31, v32, v33]]

def multiply_a_by_scalar(mat, value):
for m in range(len(mat)):
for n in range(len(mat[m])):
mat[m][n] = mat[m][n] * value

def show_matrix(mat):
print '\n'.join(' '.join(str(i) for i in row) for row in mat)

mat1 = get_matrix(1,2,3,4,5,6,7,8,9)
show_matrix(mat1)
multiply_a_by_scalar(mat1, 2)
show_matrix(mat1)

```

Hope this helps.

### #14 Simown

• Blue Sprat

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

## Re: Why can't I do this multiplication?

Posted 07 October 2011 - 06:50 AM

Haha, baavgai. Now that's good programming practice! I think we were getting there... slowly. First we need to convince the OP he wants to use lists.

### #15 Martyr2

• Programming Theoretician

Reputation: 5207
• Posts: 13,955
• Joined: 18-April 07

## Re: Why can't I do this multiplication?

Posted 07 October 2011 - 09:18 AM

I had recommended a list approach in his other thread he started about this topic. But whatever... I am done with it. Kudos to Simown for having the patience and showing outstanding effort.