0

What do I mean in this code?

def func(x): res=0 for i in range(x): res +=i return res print (func(x))

26th Jul 2016, 9:08 PM
Ahmed Kamal
Ahmed Kamal - avatar
6 Answers
+ 2
For example: if you tpye 5 as the x. Because of the 'range' method, 'i' will be 0 1 2 3 4. 'res+=i' means 'res = res+i'. Because you have created the res=0, res=res+i will become res=0+0, res=0+1, res=1+2, res=3+3, res=6+4 so it returns the final res(equal 10).
27th Jul 2016, 9:05 AM
DiYiFan
DiYiFan - avatar
+ 1
For example if you pass the 10 as the 'x' to the function 'i' will take all of the number from 0 to 9. However, because you have return yes(this bit will result in an error because yes isn't defined in python 2) the part of thw code that is under the l 5th line will never run.
26th Jul 2016, 11:24 PM
Mels Kolozyan
Mels Kolozyan - avatar
+ 1
You need put a paramenter to print your function, for example: print(func(3)) the result is 5
27th Jul 2016, 2:08 AM
George Dantas
George Dantas - avatar
+ 1
res is a variable that holds each number inside i that counts in the for loop up to the number you specify for x in range. Then as it counts it prints each one of those numbers out until it reaches the range limit, which is usually one less than the number in range.
28th Jul 2016, 11:27 PM
daniel fancher
daniel fancher - avatar
0
i know that x in print should be a number but the quistion is still what is this part of code do exactly
27th Jul 2016, 4:48 AM
Ahmed Kamal
Ahmed Kamal - avatar
0
@VonVerard, does i is mathmatical symbol like +,-,*



 . and can i use it in any other code ??? and does i has a name like (plus + and divition /)????
27th Jul 2016, 9:18 AM
Ahmed Kamal
Ahmed Kamal - avatar