0

Python

def print_nums(x): for i in range(x): print(i) return print_nums(10) I don't understand the code sequence. Can you help me?

3rd Sep 2021, 11:07 AM
yeola
6 odpowiedzi
+ 5
If you outdent 'return' to match the indentation of 'for', then the return (which you could leave out because now it does nothing) won't happen until the for loop is finished and the output is 0 1 2 3 4 5 6 7 8 9
3rd Sep 2021, 11:41 AM
David Ashton
David Ashton - avatar
+ 3
yeola I tried to run this code in playground, but local internet is iffy tonight. I am pretty sure that your output will be 0 The call of the definition specifies 10, but the specifics of the definition will only return 0 This is how it works for i in range(10): print(i) # 0 return # stops iteration So the only output is 0
3rd Sep 2021, 11:17 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
def print_nums(x): for i in range(x): print(i) print_nums(10)
3rd Sep 2021, 12:20 PM
SoloProg
SoloProg - avatar
+ 2
<★Shaurya Chauhan★>(Inspiration will draw you) I was just thinking the same thing
3rd Sep 2021, 11:08 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
I wanted to tell that there are 2 correct marked answers in sololearn how Is there a bug 🐛🤔 Rik Wittkopp David Ashton Your both answers are marked the best
3rd Sep 2021, 12:38 PM
<★Shaurya Chauhan★>
<★Shaurya Chauhan★> - avatar
0
Inicias una variable en (x) y luego declaras un ciclo que i es igual a x y imprime i y le asigna un vamos a la x de la variable
5th Sep 2021, 12:02 AM
Cristian David Alvarez Cardona
Cristian David Alvarez Cardona - avatar