0

def and return problem

what is the highest number output? def print_nums(x): for i in range(x): print(i) return print_nums(10) ###the answer is 0 here is my solution thought: range 0.1.2.3.4.5.6.7.8.9, why is 0? what is the output? def func(x): res = 0 for i in range(x): res += i return res print(func(4)) ###the answer is 6 Range 0.1.2.3 res=4+0, 4+1, 4+2, 4+3 then why 6?

14th Apr 2020, 12:35 PM
Arvonce
3 Antworten
+ 3
for i in range(x): print(i) Here if x=10 then I values in range 0 to x-1. So 0 to 9. range(min, max) if you not specify min, value then it starts from 0. If you want start from 1, (or some specific min value, mention there as) write range(1,10) In next, res=0 for i in range(4) res+=i Here, i is 0 to 3. #(4-1) Each time I added to res.. 0+0=0. 0+1=1 1+2=3 3+3=6
14th Apr 2020, 12:50 PM
Jayakrishna 🇮🇳
0
Jayakrishna thanks, I got it now
14th Apr 2020, 1:03 PM
Arvonce
0
Arvonce You're welcome
14th Apr 2020, 1:06 PM
Jayakrishna 🇮🇳