[Tutor] Need help adding a funcation
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help adding a funcation
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 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 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 getDiviso
Re: [Tutor] Need help adding a funcation
Can anyone else help with this question? On Thu, Dec 1, 2011 at 12:17 PM, Dave Angel wrote: > (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 > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help adding a funcation
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 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