[Tutor] Need help adding a funcation
Andreas Perstinger
andreas.perstinger at gmx.net
Fri Dec 2 10:55:16 CET 2011
On 2011-12-02 08:22, Michael Hall wrote:
> I am still not understanding what it is I am being asked to do.
Ok, forget about your working program and just concentrate on question 1a):
> # a) write a function, getDivisors(), that returns a list of all
> # of the positive divisors of a given number. for example -
> # result = getDivisors(24)
> # print(result)
> # would yield: "[ 1, 2, 3, 4, 6, 8, 12]"
I suppose you know how functions and lists works, do you?
You have to write a function named "getDivisors" which takes one
argument (a number) and returns a list. Nothing more, nothing less.
You started right with the line
def getDivisors(num):
but in all your attempts you have never returned a value. Do you know
how a function can return something?
In your case you have to return a list. Therefore you have to build this
list inside the function. You don't need to print the values or
calculate a sum, just add every divisor to the list. I'm sure you have
learned already how lists works, haven't you?
If you have problems understanding functions and lists, you should
re-read these parts in your learning material or in the online tutorial:
http://docs.python.org/py3k/tutorial/introduction.html#lists
http://docs.python.org/py3k/tutorial/controlflow.html#defining-functions
http://docs.python.org/py3k/tutorial/datastructures.html#more-on-lists
> I am asking if you are given the following question how would you
> write the program.
Sorry, we won't write the program for you. You have to do it yourself.
We will just try to give you some hints - if you carefully read the
links I've mentioned you'll find an example which comes close to yours :-).
Bye, Andreas
More information about the Tutor
mailing list