Pls, how can I write this same code, with def function and still get d same result? I've been trying but different result. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pls, how can I write this same code, with def function and still get d same result? I've been trying but different result.

https://code.sololearn.com/c1PH7JJq6FoZ/?ref=app

28th Apr 2021, 2:11 PM
Adibeh Esther
Adibeh Esther - avatar
4 Answers
0
because you use "return number" inside of the for loop. pop it out to the outter most level of the function and it will go through the entire loop and then print the whole list note: when you return inside of a function, that function is then complete and it moves on to the next line of code
28th Apr 2021, 2:21 PM
Slick
Slick - avatar
+ 1
Wow!!! Thanks alot. I've gotten it.
28th Apr 2021, 2:55 PM
Adibeh Esther
Adibeh Esther - avatar
0
Try it out inside that code! Let's see where the issue is, just give it a shot and even if it breaks the code, save it so we all can see why
28th Apr 2021, 2:13 PM
Slick
Slick - avatar
0
def number(): number=[] n=int(input("Enter a num")) for i in range(0,n): num=int(input("Type a num:-")) x=number.append(num) return number print(number()) #This is d one I tried it's only printing the 1st number I type, then it breaks. Unlike d 1st one that will print the full list.
28th Apr 2021, 2:17 PM
Adibeh Esther
Adibeh Esther - avatar