Re: [Tutor] Question

2011-12-03 Thread Alan Gauld

On 03/12/11 08:31, Michael Hall wrote:


# a) write a function, getDivisors(), that returns a list of all
# of the positive divisors of a given number.


This is a very clear statement of what you need to do.


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

 if num % number == 0:
 print([1, 2, 3])
 sum += num

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



problem. If you enter the number 6 the program is just what the teacher
wants. My problem is if you enter any other number it is wrong.


Even for 6 it is NOT what the teacher wants.
Your function PRINTS the answer the teacher asked for a function that 
RETURNS the answer. These are different things.

Do you understand the difference?

Also the reason it works for 6 is because you hard coded the answer for 6:

>  if num % number == 0:
>  print([1, 2, 3])

You only ever print [1,2,3]. You are not finding the divisors you are 
just printing [1,2,3].


Similarly the teacher did not ask you to test/report whether the number 
was perfect in the function, only to get the list of divisors. You need 
to strip the function back to the minimum to do what you were asked to do.


def getDivisors(num):
my_list = []
for x in range (1,num):
   # if is x a divisor of num
   # add x to my_list
return my_list

I've left the commented lines for you to complete.

But that is ALL you need for part (a) of the assignment.

You might need a main() function to test it, but it will simply print 
the result. Something like:


def main():
   print getDivisors(24)


As it is you are trying to do way too much and making your function more 
complicated than it needs to be. You can tweak it to make it more 
efficient later, but for now stick to the simplest thing that can 
possibly work and solve the problem you were asked to solve.


Once you have part (a) working part (b) of the assignment becomes easy.
But get (a) working before attempting (b). At the moment you are mixing 
the two together and making it more difficult. And that is one of the 
lessons from the homework. In programming, if you split a task down into 
smaller chunks it becomes easier. If you try to solve everything in one 
place it is much harder.


--
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


Re: [Tutor] Question

2011-12-03 Thread Walter Prins
Hello Michael,

On 3 December 2011 08:31, Michael Hall  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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] where python is used in real world

2011-12-03 Thread surya k
I don't understand why python doesn't provide executable files for the source 
code. 

I am using py2exe for building executables and I found that, it doesn't 
completely change code into binary but creates dll files and python libraries 
to run that particular code. This process makes program very large although it 
isn't.

1. Why doesn't python doesn't offer executable file ? 
2. If one wants to make a commercial software using python, how can he hide the 
code?
3. However, if one wants to make an open source software, as python doesn't 
offer executable files, how people make those so as to distribute their 
products to people?___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor