views:

114

answers:

4
#!/usr/bin/env python
import math



def primeTest(isPrime):
print('    {0}=testnum'.format(testnum))
if testnum%2 == 0 and testnum != 2: #if divisible by 2 and not 2
 isPrime = False
 print('{0} a'.format(isPrime))
 print('a') 
else:
 numroot = round(math.sqrt(testnum))
 i = 2
 while i <= numroot:
  if testnum%i == 0:
   isPrime = False
  i+=1
 print('b')


global testnum
global isPrime
testnum=2
numPrimesSoFar=0
reqPrimes=int(input('How many primes would you like? \n'))
while numPrimesSoFar < reqPrimes:   
isPrime=True
primeTest(isPrime)
print(isPrime)
if isPrime:
 print('    {0}'.format(isPrime))
 print('    {0}'.format(testnum))
 numPrimesSoFar+=1
testnum+=1

(sorry about the formatting I'm not really sure why it's not working right, but assume I have the tab-formatting correct) Now this outputs this:

    How many primes would you like? 
4
    2=testnum
b
True
    True
    2
    3=testnum
b
True
    True
    3
    4=testnum
False a
a
True
    True
    4
    5=testnum
b
True
    True
    5

Alright... so why is isPrime true still when I set it to false?

EDIT: Okay... so is THAT what you guys are talking about?

A: 

Although you have set isPrime to be a global, you are modifying it inside the local bounds of the primeTest function. As a result, it won't be modified.

If you need it changing, try passing it in as a parameter and having it modified that way.

Smallgods
Only commenting on this one because it was the most helpful but thank all you guys so much for helping me.
rdeluca
"Passing it in as a parameter and having it modified that way"...okay... so would that be something likedef primeTest(isPrime)... would I have to change anything else? I'm just loosely grasping the python language right now and any help would be awesome.
rdeluca
+3  A: 

global isPrime needs to be inside the function where you assign to isPrime.

Jason Orendorff
+2  A: 

I think that, since you are declaring the isPrime global variable after the definition of primeTest(), the Python interpreter treats the isPrime within the function as a local variable.

I was mistaken.. you have to declare it as global isPrime from within the function. Order of declaration (function or variable first) doesn't matter. Something like this:

def primeTest():
    global isPrime
    isPrime = False

isPrime = True
print isPrime # returns True
primeTest()
print isPrime # returns False

Note that you cannot write global isPrime = False; it must be two statements.

Anyway, it is rather bad practice to be changing global variables like that. Why don't you pass isPrime as an argument to primeTest()?

Edit:

Okay, my answer was still too hasty. Simply passing isPrime as an argument doesn't work, because boolean values are immutable types. The best way to handle your current problem is to have isPrime as a return value:

def primeTest(testnum):
    ... do your calculations here, set a variable ret_val to True or False ...
    return ret_val

Now you can do isPrime = primeTest(10) to find out if any number is a prime.

However, if you pass your function a mutable type (like a list), you can modify it from within the function. This post covers it quite well. But you needn't concern yourself with that just yet.

int3
Ah, I didn't notice that you suggested passing isPrime as an argument as well, once I pass it in when the function completes will the isPrime be updated?Also, thank you very much for the help.
rdeluca
+1  A: 

global foo means, in this scope, let me say foo to refer to a foo accessible from outside this scope and persistent after this scope closes. It does not mean, let all variables named foo everywhere and forever refer to this foo.

This is different from what you might expect if you're coming from, say, a C/C++ background where declaring a variable at the top level makes it global without requiring the explicit global declaration in each scope.

Wang
yeah... c/C++ and java. Sigh.
rdeluca
AH! Fixed. Thanks. Awesome. Just printed the first 10,000 primes and they're all right. Wohoo...
rdeluca