20 Answers
New Answerdef my_func(start, end): return [i**2 for i in range(start, end)] print(my_func(1, 31))
Okay, elvisreal, now we see a bit what has been the problem with this thread to begin with, although nobody cared back then. You came with a question that read like 'write that code for me'. And instead of telling you that you should try it yourself, they all just dished out the solution to you. And now see what has happened: Three full months later, you still don't even know that you have to print out a return value if you want to see it! There's nothing bad about being at whatever point you are, because everybody was there once. But do you want to stay there forever? And to the other people: Do you want this person to remain a beginner for all eternity? Coding can only be learned by writing code with your own hands! So write your codes yourself! And make other people write their codes themselves!
Honfu thanks for your opinion, but coding cant only be learned that way, because people are different and they learn differently..Its just like teaching a child how to ride a bike, sooner or latter the child starts to ride its own bike.. i know I'm a beginner, for a month and half I had to leave programing, to get something very important to me. But I'm back now and NO i will never stay a beginner for eternity.
If You are doing this for learning purpose rhen it ohk but if it is for homework.... you are going wrong. x=[] for i in range(1,31): x. append(i**2) print(x)
A more pythonic function, using list comprehension: def my_func(start, stop): return [i**2 for i in range(start, stop)] I added a small perk, that allows you to do what you want, but also to specify other start and stop values ;) For your objective: my_func(1, 31) Happy coding!
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message