[Tutor] Need help adding a funcation

Michael Hall michael.hall5447 at gmail.com
Thu Dec 1 20:55:56 CET 2011


The OP has been taught but is still having an issue and all I am doing is
asking for help. Here is what I have so far

# 1) a perfect number is a positive integer that is equal to the
# sum of its proper positive divisors,excluding the number itself.
# for example, 6 is a perfect number because it is evenly divisible
# by 1, 2 and 3 - all of it's divisors - and the sum 1 + 2 + 3 = 6.
# 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]"

def main():
    x = 1

    num = int(input('Please Enter a Number: '))
    getDivisors(num)

def getDivisors(num):
    sum = 0
    x = 1
    #my_list[] = num
    for num in range(1, num, 1):

        if num % x == 0:
            print(num)
            sum += num

    print('The sum is ', sum)
    if sum == num:
        print(num, 'is a perfect number')
    else:
        print(num, 'is not a perfect number')

main()
On Thu, Dec 1, 2011 at 10:57 AM, Dave Angel <d at davea.name> wrote:

> (You forgot to post your response on the list, instead posting it
> privately to me.  Please use Reply-All, or whatever the equivalent is on
> your email app)
>
>
> On 12/01/2011 11:49 AM, Michael Hall wrote:
>
>> On Thu, Dec 1, 2011 at 7:51 AM, Dave Angel<d at davea.name>  wrote:
>>
>>  On 12/01/2011 10:33 AM, Michael Hall wrote:
>>>
>>> Here is the code I have written.
>>>>
>>>> # Create main function.
>>>> def main():
>>>>     a = input('Please Enter a Number: ') # Ask user for input.
>>>>     number = int(a)
>>>>     x = 1
>>>>     sum_of = 0
>>>>     while number>   x:
>>>>         if number % x == 0:
>>>>             sum_of = sum_of + x
>>>>         x += 1
>>>>     if sum_of == number:
>>>>        print(number,'is a Perfect Number')
>>>>     elif sum_of<   number:
>>>>        print(number,'is a Non-perfect Number')
>>>>
>>>> main()
>>>>
>>>> Here is the problem I am having. The program works perfect but I need to
>>>> add the following:
>>>>
>>>> # 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]"
>>>> # b) write a program that uses your function to determine which
>>>> # numbers between 1 and 10,000 are perfect. (answer: 6, 28, 496
>>>> # and 8128 are perfect!)
>>>>
>>>> I know that mystring needs to be added. I need help
>>>> Thank you in advance
>>>>
>>>> I don't see any 'mystring' in the source or in the assignment;   why
>>>>
>>> would you think you need it?
>>>
>>> It asks you to write a function called getDivisors().  You have all the
>>> pieces in your present code, but it's all inline.  Probably your
>>> professor
>>> wants you to learn to code in small functions, so your code is more
>>> likely
>>> to be reusable.
>>>
>>> So what part of the function description is unclear?  And is it unclear
>>> because you don' t understand one of the math terms, or because you don't
>>> know how to code it?    She wants a function that takes a single argument
>>> (number), and returns a list
>>>
>>> def getDivisors(number):
>>>       do some stuff
>>>       return mylist
>>>
>>> You'll probably want to write another one that adds up the elements of a
>>> list (or you could find it in the stdlib).  Then your main should be
>>> pretty
>>> straightforward.
>>>
>>>
>>>
>>> --
>>>
>>> Dave
>>>
>>
>
> A
>
>>
>>> The truth is I am unsure how to code it. Here is my guess:
>>>
>>
>> def main():
>>     a = input('Please Enter a Number: ') # Ask user for input.
>>     number = int(a)
>>     x = 1
>>     sum_of = 0
>>     while number>  x:
>>         if number % x == 0:
>>             sum_of = sum_of + x
>>         x += 1
>>     if sum_of == number:
>>        print(number,'is a Perfect Number')
>>     elif sum_of<  number:
>>        print(number,'is a Non-perfect Number')
>> def getDivisors(number):
>>            while count != num:
>>         if (num % count) != 0:
>>             mylist.append(count)
>>             count +=1
>>         else:
>>             count +=1
>>     print(mylist)
>>
>>
>> main()
>>
>>
>> YES, I DO NOT UNDERSTAND HOW OR WHERE TO PUT THE CODE.
>> THANK YOU FOR YOUR HELP.
>>
>>
> You want a separate function, so don't try to put it in the middle of the
> present one.  Put the def *after* the print(mylist) line.
>
> All you can take from the older function is ideas.  Since you wrote it,
> you must know how to write the new one.  The task is much simpler, and is a
> subset of the task the first one did.
>
> You're going to need some form of loop.  Figure out what the limits are
> and see if you can figure out some way to do a for loop between those
> limits.  The work of the loop body will be done by the same
>
>      if number % x == 0:
>
> that you had before, but you do something different than what you did
> before.  Your job is to build a list.
>
> Note to others:  Don't confuse the issue with efficiency, nor with some
> library code.  This is a homework assignment, and it has to be done with
> concepts the OP already has been taught.
>
> --
>
> DaveA
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111201/ba16ad46/attachment-0001.html>


More information about the Tutor mailing list