+ 1
can anyone elaborate and explain this ?
What is the output of this code? def func(x): res = 0 for i in range(x): res += i return res print(func(4))
4 Réponses
+ 14
Not a Python expert but these are basics of every programming language! ^~^
https://code.sololearn.com/cVfG0h0YbPd8/?ref=app
+ 5
//The output is: 6
Trying explainations:
1- def func(x)
/*defining a function named "func" with an argument "x", i beleive u know it, dont worry!*/
2- res=0
//initialising variable "res" to 0
3- for i in range(x)
/*looking for all the first 4th int (starting from 0 to argument x entered below, with a variable "i" used to store or define the index of each of those elements*/
4- res+=i
/*while loking for those numebers, adding each time to "res" the new value of "i" the element's index*/
5- return res
//returning the final value of res at the loop end
6- print(func(4))
/*calling the "func" function with the parameter 4, that will repace "x" in the function execution, and print the returned value*/
//execution:
res=0
res=res+i (0+0)
res=res+i (0+1)
res=res+i (1+2)
res=res+i (3+3)
res==6
Hope i helped u anought
+ 2
i didn't saw @Maz forestallen me
0
thank you both of u