[Tutor] Question

Walter Prins wprins at gmail.com
Sat Dec 3 11:14:25 CET 2011


Hello Michael,

On 3 December 2011 08:31, Michael Hall <michael.hall5447 at gmail.com> wrote:

> Andreas and others "Thank you for your help. I am still having an issues.
> I went to the links and they were very helpful.

Would you mind explaining exactly how they helped you? :)


> Here is my problem. If you enter the number 6 the program is just what the
> teacher wants.

Well, to be pedantic no, it's not just what the teacher wants.  The problem
states:
    # 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]"

As your solution stands, the function getDivisors(num) that you have posted
does not return anything at all.   So you've not actually solved part a) of
this problem yet at all.

In the solution of part a) you should have the above snippet of code
somewhere in your program, and it should return the list as described when
run.

My problem is if you enter any other number it is wrong.

It is wrong because you have the answer for 6 hardcoded inside of your
getDivisors() function.  In other words, the *only* list your function can
return is [1,2,3] as that's what it returns explicitly.  To belabor the
point further: You never actually build the result list you want to return
later in the function.

Also, this problem is actually trying to work/solve the bigger problem of
deciding whether a given number input by the user is actually a perfect
number, rather than the first problem of writing a function to find the
divisors of a given number.   However, you simply cannot go and work on the
bigger problem before you have a demonstrably working solution to part a),
that is, a working getDivisors() function that actually returns a list of
divisors for a given number, correctly, for arbitrary input.

Do not pass go, do not collect $200 or £200 before you've completed this
task.  ;)

I would submit your real problem is not that the numer returned is wrong,
the real problem is that you don't seem to understand the basic building
blocks needed to solve this problem e.g:
a) how to use function calls to return results
b) working with Python lists (putting items into them, passing them around
etc.)

You absolutely must get a conceptual handle on calling functions and
returning results and working with lists in order to be able to solve this.

If I put the following requirement to you:
"Write a function that returns the square of the number given to it"
... would you be able how to solve it?  If not, what problems do you run
into/what do you not understand?

If I then put the following requirement to you:
"Write a function that will return the square of every number in the list
given to it (as a return list)"
... would you be able to solve it? If not, what problems do you run
into/what do you not understand?

I'd suggest trying the above questions and posting back your solution(s) to
them to help us establish that you do understanding functions and lists.

Finally, allow me to point out that you're top posting, that is, you're
posting your responses to previous emails at the top.  Please note that the
convention on this list is to bottom-post, or at least to follow responses
**below** relevant parts of emails inline, hence preserving the context in
easy reading order for both the participants and people who may not have
followed/read the question/answers provided up to that point.  If some
people bottom post and some top post it can become very difficult to follow
who said what when and makes helping on a list like this more work than it
would otherwise be.  Please help the people on this list trying to help you
by respecting this convention, thanks.

Cheers,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111203/cae70241/attachment-0001.html>


More information about the Tutor mailing list