0
def FindingTwoPrimes(n, p1, p2):
    p1 = 0
    p2 = 0
    n = 0
    primeList = []
    for num in range(1,101):
        if all(num%i!=0 for i in range(2,num)):
            primeList.append(num)
    print (primeList)
    print ("Length of Primelist = ", len(primeList))
    lengthOfPrimelist = len(primeList)
    p1 = primeList[random.randint(0,lengthOfPrimelist)]
    p2 = primeList[random.randint(0,lengthOfPrimelist)]
    n = p1 * p2
    print ("p1 =", p1)
    print ("p2 =", p2)
    return n
    return p1
    return p2


FindingTwoPrimes(n, p1, p2)
print(p1, p2, n)

So above is a function that will find two primes that I will use in the method of the public key encryption system. However, when I run the function it returns p1, p2 and n back as 0. Why is this and how do I solve it? Also, any methods to make this code more efficient would be appreciated :-)

1
  • 2
    Use return (n, p1, p2) and then get them as: n, p1, p2 = FindingTwoPrimes(n, p1, p2) Commented Oct 28, 2017 at 17:04

3 Answers 3

4

Your function will return single value, because it execution will be finished after first return statement. You could return multiply values using a tuple

def FindingTwoPrimes(n, p1, p2):
...
    return n, p1, p2


n, p1, p2 = FindingTwoPrimes(n, p1, p2)
print(p1, p2, n)

Also, your code wouldn't work at all. In the beginning of function you assign zero values to all it args.

def FindingTwoPrimes(n, p1, p2):
    p1 = 0
    p2 = 0
    n = 0

Try to use variables with different names inside function.
As I understand, you trying to pass results back from function in arguments. This is bad idea. Use tuple to get results from function

def FindingTwoPrimes():
    p1 = 0
    p2 = 0
    n = 0
    ...
    return n, p1, p2

n, p1, p2 = FindingTwoPrimes()
print(p1, p2, n)

About last part of your question - how to make it more efficient. If you planning to call this function many times you could move primeList calculation outside of function.

def FindingTwoPrimes(primeList):
    lengthOfPrimelist = len(primeList)
    p1 = primeList[random.randint(0,lengthOfPrimelist)]
    p2 = primeList[random.randint(0,lengthOfPrimelist)]
    n = p1 * p2
    print ("p1 =", p1)
    print ("p2 =", p2)
    return n, p1, p2

primeList = [num for num in range(1,101) if all(num%i!=0 for i in range(2,num))]

print (primeList)
print ("Length of Primelist = ", len(primeList))

n, p1, p2 = FindingTwoPrimes(primeList)
print(p1, p2, n)
Sign up to request clarification or add additional context in comments.

Comments

1

There should be only one return in a function but you have 3. Change the below lines from

return n
return p1
return p2

to

return [n,p1,p2]

then change print statement as

print FindingTwoPrimes(n, p1, p2)

Also code in your function is wrong. Please change that as well

import random
def FindingTwoPrimes(n, p1, p2):
    primeList = []
    for num in range(1,101):
        if all(num%i!=0 for i in range(2,num)):
            primeList.append(num)
    lengthOfPrimelist = len(primeList)
    p1 = primeList[random.randint(0,lengthOfPrimelist)]
    p2 = primeList[random.randint(0,lengthOfPrimelist)]
    n = p1 * p2
    return [p1,p2,n]

print FindingTwoPrimes(10, 11, 12)

2 Comments

Why do you pass arguments to a function, if they are not used inside it?
Could be OP wants to do some more things inside function with first value n. That's why i didn't remove.
-1

for solve this only need

(n, p1, p2) = FindingTwoPrimes(n, p1, p2)

1 Comment

That's not the only change needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.