On 05/13/2013 07:01 PM, Daniel Magruder wrote:
Dear Dave,
I am using python 2.
I am still confused as what return does. What does it mean if a
> function returns True to the caller? What is the caller?
Have you ever used (called) a function? If so, you've written a caller.
For example, if you want the absolute value of an integer, you might do:
import math
x = float(raw_input("Type an integer"))
y = math.fabs(x) #Here I'm calling the function
print "Absolute value is", y
Now, somewhere in the Python library, a function was defined called
fabs(), and it had a return statement with the value you want.
Your code worked for returning a list of 1000 items of odd numbers, so I then
tried writing a code to replay isodd to give True or False for isprime. However,
def isodd(candidate):
if candidate%2 ==0:
return False
else:
return True
The meaning of this I am still confused about. What does it mean if the value
is returned as false if it has 0 remainder, what does it mean if it is returned
as true? Is this like a dictionary with keys and values?
I don't have a clue what you're confused about. Do you not understand
remainders? If the remainder is 1 then the number is odd, while if the
remainder is 0 then the number is even. That's math, not programming.
Also, here is my new code, which still does not work. I have no idea why not,
and am getting very frustrated by the fact that making a program that
determines if a number is prime or not is so hard for me. I would appreciate
any assistance, and a verbal walk through of how to logically determine if a
number is prime.
def isprime(x):
test=2
numberinquestion = x
while test < numberinquestion:
if numberinquestion % test == 0:
test += 1
return False
elif numberinquestion / test == int:
Here you go again; int is a type, not a value. Anyway, all the testing
you needed was already done, if there's a nonzero remainder you already
returned a False. If there is a remainder, there's nothing more to test
for this value of 'test'. So increment and loop around.
test += 1
return False
if test == numberinquestion:
This is also unneeded. If the while has finished, then you can return True.
return True
def counting_primes():
prime = 2
primelist=[]
while len(primelist) < 1000:
if isprime(prime):
primelist.append(prime)
prime += 1
return primelist
Sincerely,
Dan
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor