I am still not understanding what it is I am being asked to do. What is the differance between my_list = [] an my_list[ ] because when I use my_list[] I get an error. Not sure what I am doing wrong. I am asking if you are given the following question how would you write the program. I have most of it. Here is what I know works # Create main function. def main(): a = input('Please Enter a Number: ') # Ask user for input. number = int(a) x = 1 sum_of = 0 getDivisors 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(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() This is what the teacher is asking for: # 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]" # 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 am asking for help writing it. I am new to programming. Lost alot of brain matter. On Thu, Dec 1, 2011 at 3:22 PM, Alan Gauld <alan.ga...@btinternet.com>wrote: > On 01/12/11 21:17, Michael Hall wrote: > >> Can anyone else help with this question? >> > > Sure lots of us could help. But Dave's doing a good job > leading you in the right direction. > > Is there something that you don't understand? If you ask about specific > points we can give specific answers. Meantime I'll add a few extra comments > to see if that helps... > > > 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. >> > > What Dave means is that you are simply calling the function but not > storing the result. So you calculate the divisors but then have no chance > to use them. You need to store them in a variable someplace. > Think about the input() function you use above. > > If you had just done > > > int(input('Please Enter a Number: ')) > > You couldn't use the value that the user entered. > You had to assign it to num to do that. It's the same > with your function getDivisors() > > > def getDivisors(num): >> sum = 0 >> x = 1 >> #my_list[] = num >> >> That's close. To create an empty list, simply do >> my_list = [] >> > > self explanatory, I hope. > > > > 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? >> > > So you need to store your results as a list not as a number. So instead of > sum() what can you do to my_list to add a new element? > If you aren't sure fire up the Python >>> prompt and try > help(list) or just help([]) and see if you can see anything useful. > > > 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. >> > > This is a good principle to apply when writing functions. Separate out the > display from the logic. That way you can use the results of the logic with > any kind of user interface - GUI, Web, or command line, or even in a server > process without a UI. > > But this takes us back to the advice to assign the result to a variable in > main(). > > To do that you need to return a result. And in this case your result > should be a list of numbers. then you can do any printing in the main > function. > > HTH, > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor> >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor