Answered # squaring numbers

masterofpuppets 19 Discussion Starter gangster88 masterofpuppets 19 masterofpuppets 19 Discussion Starter gangster88 masterofpuppets 19 Discussion Starter gangster88 masterofpuppets 19 Discussion Starter gangster88 masterofpuppets 19 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

hi,

here's a way of doing it:

```
def square( first, last ):
total = 0
for i in range( first, last + 1 ):
total += i**2
return total
print square( 3, 5 )
```

0

hi,

here's a way of doing it:`def square( first, last ): total = 0 for i in range( first, last + 1 ): total += i**2 return total print square( 3, 5 )`

thankyou very much for the help, but could you please explain the last 2 lines.. coz we have not done tht yet and there is want to show work which i cant explain..

0

right,

here's what happens here:

total holds the sum of all squared values, so the first time i = 3 so i**2 = 9, and total += i**2 means total = total + 9. The value for total here is 0 so total = 0 + 9 and now total is 9.

The second time i**2 = 16 so total = total + i**2 is 9 + 16 and therefore total = 25 now.

And the third time i**2 = 25 so total = 25 + 25 and total = 50

All we need to do now is to return it

return total

and that's it.

Hope this helps :)

If not I'll try to explain it better :)

0

P.S.

probably the '+=' operation is confusing you here.... total += 1 simply means total = total + 1

:)

0

P.S.

probably the '+=' operation is confusing you here.... total += 1 simply means total = total + 1:)

so if the + operation was not there then would it have outputted 9? because it would just do 3**2 and not add the rest?

0

so if the + operation was not there then would it have outputted 9? because it would just do 3**2 and not add the rest?

yes but at the end of the loop you would get 25 because the last value of i is 5, i.e. 5**2 = 25 so the value of total becomes 25

0

P.S.

probably the '+=' operation is confusing you here.... total += 1 simply means total = total + 1:)

yes but at the end of the loop you would get 25 because the last value of i is 5, i.e. 5**2 = 25 so the value of total becomes 25

so would that mean it would do 3**2, 4**2 and 5**2 but it would add 9+16+25 and instead it would just output 25 as it at the end of the loop?

0

if you write the function like this:

```
def square( first, last ):
total = 0
for i in range( first, last + 1 ):
total = i**2 # Not total += i**2
return total
print square( 3, 5 )
```

it would output this:

```
>>>
25
>>>
```

because you are not adding the result of every square to total, i.e total becomes whatever the result for the last value of i squared is, since i is 5 at the end, total becomes i**2 which is 25 :)

0

yup thts exactly wht i meant... and + would mean it squares every number and add it to the total and moves on the next without leaving any out.. yup tx for the help... appreciate it... nw i get it..

but one last question, why is the total=0? is tht like a rule that it has to equal 0?

*Edited 7 Years Ago by gangster88*: n/a

0

yup thts exactly wht i meant... and + would mean it squares every number and add it to the total and moves on the next without leaving any out.. yup tx for the help... appreciate it... nw i get it..

but one last question, why is the total=0? is tht like a rule that it has to equal 0?

well when you want to count stuff or find a sum of numbers you need the count, or whatever variable name you use, to be initially 0 because if it's 1 or more or 0 and less you will get incorrect results. Basically this is what I mean:

>>>total = 1

>>>total = total + 5**2

>>>print total

26 # but 5**2 is 25 so total has to be 0

the statement total = total + 5**2 means compute the sum of the current value of total and the square of 5 and replace the current value of total with the result of the computation :)

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```