+ 10
I don't know what is your question exactly.. Your code: def print_nums(x): for i in range(x): print(i) return print_nums(10) prints 0 only. If you do want to print from 0 - 9 remove return from the function (i.e) def print_nums(x): for i in range(x): print(i) print_nums(10) And the program flow will like the 4th line print_nums(10) calls the function and send 10 as argument. In function x value will be 10 and the for loop will run 0-9 times and print the i. I hope you understood. :D
31st Mar 2017, 4:34 AM
Mr.Robot
Mr.Robot - avatar
+ 9
def print_nums(x): for i in range(x): print(i) return print_nums(10) Here , why 0 only is printing is you're returning after printing 0. So it will come out of the function. Does it makes sense?
31st Mar 2017, 4:40 AM
Mr.Robot
Mr.Robot - avatar
+ 2
Line 1: You define a function called print_nums that takes 1 argument. (x) Line 2: You setup a for loop inside the function, that iterates through the range of the variable x, using the variable i. Line 3: Inside the for loop, you print i. Line 4: Inside the for loop, you exit the function via "return". Line 5: You call the function print_nums with the argument 10. When you indent the last line, you don't get an output because you're trying to call the function from within the function. When you don't indented the last line, you get an output of 0 because you print(i) inside the for loop, and on the first run of the for loop, i == 0. 1-9 doesn't print because you return (exit the function) after the first run of the for loop. If you move the return out of the for loop, 0-9 will print: def print_nums(x): for i in range(x): print(i) return print_nums(10)
31st Mar 2017, 4:41 AM
Martian1oh1
Martian1oh1 - avatar