[Tutor] Need help adding a funcation
Dave Angel
d at davea.name
Thu Dec 1 21:17:09 CET 2011
(You top-posted, so I'm deleting all the out-of-order stuff. in these
forums, you should put your response after whatever you quote.)
On 12/01/2011 02:55 PM, Michael Hall wrote:
> 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)
You'll want to store the return value of getDivisors, since you have
more work to do there.
>
> def getDivisors(num):
> sum = 0
> x = 1
> #my_list[] = num
That's close. To create an empty list, simply do
my_list = []
> for num in range(1, num, 1):
>
> if num % x == 0:
> print(num)
> sum += num
Why are you summing it? That was in another function. In this one,
you're trying to build a list. Any ideas how to do that at this point
in the function?
>
> print('The sum is ', sum)
> if sum == num:
> print(num, 'is a perfect number')
> else:
> print(num, 'is not a perfect number')
None of these lines belong in your function. All it's supposed to do is
get the divisors, not to study them in any way.
>
> main()
--
DaveA
More information about the Tutor
mailing list