-1

I have function which checks the score and will(I haven't finished) increases level if the score hits the given score, my function:

def levels(Score):
 if score >= 100:
   enemies = 6
   velocity = 2

and I'm calling it in the game loop:

levels(score)

The function never gets executed, my source code http://pastebin.com/JPZSTA6a Line: 35-38 and 150

Thank you

ErHunt
  • 311
  • 2
  • 9
  • 18
  • 2
    What is your evidence for the claim that "the function never gets executed"? – Karl Knechtel Apr 28 '12 at 00:36
  • 1
    Have you tried putting a `print` statement in the function to test whether or not it is called? – Wes Apr 28 '12 at 00:38
  • @KarlKnechtel because if the function is executed it will throw up an error message. – ErHunt Apr 28 '12 at 00:42
  • If I were you, I wouldn't put functions inside a function unless it requires to be in. They can be put outside. It makes you code easier to debug. And you wrote `score` instead of `Score`. – CppLearner Apr 28 '12 at 00:49

4 Answers4

2

You have if score >= 100 when you probably meant if Score >= 100. The function gets executed, it's just that the if statement always evaluates to false.

Marlon
  • 19,924
  • 12
  • 70
  • 101
  • that was one of the errors, but still not working. the if statement if placed in the game loop does execute. – ErHunt Apr 28 '12 at 00:44
2

The function is being called, but you are assigning to enemies and velocity in the function, so they are local to the function, and are then discarded when the function returns. So your function is called, but has no lasting effect.

You need to read about locals and globals in Python. As others point out you also have both Score and score here. Python is case-sensitive, those are different names.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
1

It's a scoping issue, the variables you're referring (enemies and velocity) to are created further down inside a while loop so they're not in scope in the function where you're trying to modify them. You need to read up on Execution model it should be able to clarify scoping rules.

John Gaines Jr.
  • 11,174
  • 1
  • 25
  • 25
  • I tried placing score,enemies and velocity in the game() but still the func does not get called – ErHunt Apr 28 '12 at 01:01
0

Your code, paraphrased:

def game():

    def levels(Score):
        print 'levels!'

    while start == True:
        print 'start is indeed True!'
        while not finish or falling: 
            'I am not Finnish'     
            for i in range(enemies):
                'alas for I am beset by enemies!'
                levels(score)

So, why isn't levels() being called? I suppose one of those many control flow items doesn't go the way you want. I can't say for example whether or not your enemies variable is empty but I can tell you that print is your friend.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47