开发者

Python Programming Help

#!/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?


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜