This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-07-21 11:27:48

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Python Help

I need some help debugging this game.  I've tried everything I know, but nothing works.  I use Python 3.1.4.  Thanks guys!

import random

#Setting global variables and such items
loop = 1
level = 1
win = 0
move = ''' '''

#Setting variables and such items for Bulbasaur (player):
hp = 200
armr = 10
sparmr = 10
atk = 5
spatk = 10
speed = 0

pp1 = 20
pp2 = 40
pp3 = 10
pp4 = 15

gold = 0

#Setting variables and such items for Dark Bulbasaur (enemy):
ehp = (190 + level)
earmr = 10
esparmr = 5
eatk = 10
espatk = 10
espeed = 0

def setVar():
    #Setting global variables and such items:
    loop = 1
    level = 1
    win = 0
    move = ''' '''
   
    #Setting variables and such items for Bulbasaur (player):
    hp = 200
    armr = 10
    sparmr = 10
    atk = 5
    spatk = 10
    speed = 0
   
    pp1 = 20
    pp2 = 40
    pp3 = 10
    pp4 = 15
   
    gold = 0
   
    #Setting variables and such items for Dark Bulbasaur (enemy):
    ehp = (190 + level)
    earmr = 10
    esparmr = 5
    eatk = 10
    espatk = 10
    espeed = 0

    global speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk

#The function that runs the game:

def playerTurn():
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    move = input('''What will Bulbasaur do?\n\n>>> ''')
    move = move.lower()
    playerMove(move)

#The function that processes the player's chosen move:

def playerMove(move):
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    if move == '''tackle''':
        if pp1 > 0:
            print('''Bulbasaur has used Tackle!''')
            atk = random.randint(0, 100)
            if atk < (90 + speed):
                ehp -= (20 + atk - earmr)
                print('''Dark Bulbasaur was hurt!\n''')
            else:
                print('''Bulbasaur missed!''')
            pp1 -= 1
        else:
            print('''Bulbasaur does not have enough PP to perform Tackle.\n''')
            playerTurn()
    elif move == '''growl''':
        if pp2 > 0:
            print('''Bulbasaur has used Growl!''')
            atk = random.randint(0, 100)
            if atk < (100 + speed):
                eatk -= 5
                print('''Dark Bulbasaur was weakened\n''')
            else:
                print('''Bulbasaur missed!\n''')
            pp2 -= 1
        else:
            print('''Bulbasaur does not have enough PP to perform Growl.\n''')
            playerTurn()
    elif move == '''leech seed''':
        if pp3 > 0:
            print('''Bulbasaur used Leech Seed!''')
            atk = random.randint(0, 100)
            if atk > (90 + speed):
                ehp -= (10 + spatk - esparmr)
                hp += (10 + spatk - esparmr)
                print('''Dark Bulbasaur was drained!\n''')
            else:
                print('''Bulbasaur missed!\n''')
            pp3 -= 1
        else:
            print('''Bulbasaur does not have enough PP to perform Leech Seed.\n''')
            playerTurn()
    elif move == '''vine whip''':
        if pp4 > 0:
            print('''Bulbasaur used Vine Whip!''')
            atk = random.randint(0, 100)
            if atk > (100 + speed):
                ehp -= (20 + spatk - esparmr)
                print('''Dark Bulbasaur was hurt!\n''')
            else:
                print('''Bulbasaur missed!\n''')
        else:
            print('''Bulbasaur does not have enough PP to perform Vine Whip.\n''')
            playerTurn()
        pp4 -= 1
    else:
        print('''That is not a recognized move.  Please chose from the following moves:\n\t
    Tackle\n\t
    Growl\n\t
    Leech Seed\n\t
    Vine Whip\n
    If these commands do not work, check the spelling.\n''')
        playerTurn()

#The function that controls the Dark Bulbasaur's move:

def enemyMove():
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    atk = random.randint(0, 100)
    if atk <= 25:
        print('''Dark Bulbasaur used Tackle!''')
        atk = random.randint(0, 100)
        if atk <= (20 + espeed):
            hp -= (35 + eatk - armr)
            print('''Bulbasaur was hurt!\n''')
        else:
            print('''Dark Bulbasaur missed!\n''')
    elif atk > 25 and atk <= 50:
        print('''Dark Bulbasaur used Growl!''')
        atk = random.randint(0, 100)
        if atk <= (100 + espeed):
            atk -= 5
            print('''Bulbasaur was weakened!\n''')
        else:
            print('''Dark Bulbasaur missed!\n''')
    elif atk > 50 and atk <= 75:
        print('''Dark Bulbasaur used Leech Seed!''')
        atk = random.randint(0, 100)
        if atk <= (90 + espeed):
            hp -= (10 + espatk - sparmr)
            print('''Bulbasaur was drained!\n''')
            ehp += (10 + espatk)
        else:
            print('''Dark Bulbasaur missed!\n''')
    elif atk > 75:
        print('''Dark Bulbasaur used Vine Whip!''')
        atk = random.randint(0, 100)
        if atk <= (100 + speed):
            hp -= (20 + espatk - sparmr)
            print('''Bulbasaur was hurt!\n''')
        else:
            print('''Dark Bulbasaur missed!\n''')
    else:
        print('''There is a glitch in the program.  Please report the event to *name removed*.''')
        loop = 0

#The function that reports the HP and PP of the Bulbasaur and Dark Bulbasaur:

def reportStats():
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    print('''Bulbasaur has ''', hp, ''' HP!\n\nBulbasaur has ''', pp1, ''' PP for Tackle!\nBulbasaur has ''', pp2, ''' for PP for Growl!\nBulbasaur has ''', pp3, ''' PP for Leech Seed!\nBulbasaur has ''', pp4, ''' PP for Vine Whip!\n''')
    print('''Dark Bulbasaur has ''', ehp, ''' HP!\n''')

#The function that checks to see whether someone is KO'ed:

def reportDeath():
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    if hp <= 0:
        print('''Bulbasaur has been KO'ed!''')
        loop = 0
    elif ehp <= 0:
        print('''Dark Bulbasaur has been KO'ed!''')
        atk = random.randint(0, 11)
        gold += atk
        win = 1

#The function that runs the shop feature:

def shop():
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    print('''Welcome to the shop!  Feel free to look around!''')
    print('''\n
\tAttack Kit 1: +1 to ATK, 25 Gold\n
\tAttack Kit 2: +5 to ATK, 125 Gold\n
\tAttack Kit 3: +10 to ATK, 250 Gold\n
\tSp Attack Kit: +1 to Sp. ATK, 25 Gold\n
\tSp Attack Kit: +5 to Sp. ATK, 125 Gold\n
\tSp Attack Kit: +10 to Sp. ATK, 250 Gold\n
\tDefense Kit 1: +1 to DEF, 25 Gold\n
\tDefense Kit 2: +5 to DEF, 125 Gold\n
\tDefense Kit 3: +10 to DEF, 250 Gold\n
\tSp Defense Kit 1: +1 to Sp. DEF, 25 Gold\n
\tSp Defense Kit 2: +5 to Sp. DEF, 125 Gold\n
\tSp Defense Kit 3: +10 to Sp. DEF, 250 Gold\n
\tSpeed Kit 1: +1 to Speed, 25 Gold\n
\tSpeed Kit 2: +5 to Speed, 125 Gold\n
\tSpeed Kit 3: +10 to Speed, 250\n
\tQuit: Exits the shop\n

\nWhat will Bulbasaur buy?''')
    atk = input('''>>> ''')
    atk = atk.lower()

    if atk == '''attack kit 1''':
        if gold >= 25:
            atk += 1
            gold -= 25
            print('''Bulbasaur bought the Attack Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''attack kit 2''':
        if gold >= 125:
            atk += 5
            gold -= 125
            print('''Bulbasaur bought the Attack Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''attack kit 3''':
        if gold >= 250:
            atk += 10
            gold -= 250
            print('''Bulbasaur bought the Attack Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''sp attack kit 1''':
        if gold >= 25:
            spatk += 1
            gold -= 25
            print('''Bulbasaur bought the Sp Attack Kit 1.\nBulbasaur now has ''', gold, ''' Gold''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''sp attack kit 2''':
        if gold >= 125:
            spatk += 5
            gold -= 125
            print('''Bulbasaur bought the Sp Attack Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''sp attack kit 3''':
        if gold >= 250:
            spatk += 10
            gold -= 250
            print('''Bulbasaur bought the Sp Attack Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''defense kit 1''':
        if gold >= 25:
            armr += 1
            gold -= 25
            print('''Bulbasaur bought the Defense Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''defense kit 2''':
        if gold >= 125:
            armr += 5
            gold -= 125
            print('''Bulbasaur bought the Defense Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''defense kit 3''':
        if gold >= 250:
            armr += 10
            gold -= 250
            print('''Bulbasaur bought the Defense Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''sp defense kit 1''':
        if gold >= 25:
            sparmr += 1
            gold -= 25
            print('''Bulbasaur bought the Sp Defense Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''sp defense kit 2''':
        if gold >= 125:
            sparmr += 5
            gold -= 125
            print('''Bulbasaur bought the Sp Defense Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''sp defense kit 3''':
        if gold >= 250:
            sparmr += 10
            gold -= 250
            print('''Bulbasaur bought the Sp Defense Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''speed kit 1''':
        if gold >= 25:
            speed += 1
            gold -= 25
            print('''Bulbasaur bought the Speed Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''speed kit 2''':
        if gold >= 125:
            speed += 5
            gold -= 125
            print('''Bulbasaur bought the Speed Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''speed kit 3''':
        if gold >= 250:
            speed += 10
            gold -= 250
            print('''Bulbasaur bought the Speed Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
            shop()
        else:
            print('''Bulbasaur does not have enough Gold to buy that.''')
            shop()
    elif atk == '''quit''':
        win = 0
        level += 10
        print('''You have reached Level ''', level / 10, '''.\n''')
        setVar()

#The function that runs the entire game- funny how simple you can make code with 'def' functions:

def runGame():
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    if win == 0:
        playerTurn()
        enemyMove()
        reportStats()
    elif win == 1:
        shop()

#The part which causes the whole game to go into effect- funny how it's so short and yet so important:

while loop == 1:
    global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
    runGame()

Hm.  Now when I run this, when someone gets KO'd, nothing happens.  It just keeps looping through and saying that someone has 0 HP.  Any help?

Last edited by maxskywalker (2011-12-19 10:19:20)

Offline

 

#2 2011-07-21 11:30:15

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Python Help

What exactly doesn't work?


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#3 2011-07-22 08:50:03

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Python Help

Harakou wrote:

What exactly doesn't work?

When you try to type a move, it gives you an error.  Try it.

Offline

 

#4 2011-07-22 09:37:57

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Python Help

Well, I just found something very baffling. The error I'm getting is that the pp variables are called but not defined, which I thought was odd because you define those in the beginning of the script. So, I was running it in the debugger, and everything goes along fine with variables and functions being declared until you actually call the runGame() function. At that point, all the variables inexplicably disappear. I really have no idea why it's doing this, but let me look into it a bit further.


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#5 2011-07-22 09:43:28

demosthenes
Retired Community Moderator
Registered: 2008-02-19
Posts: 1000+

Re: Python Help

I have not done any python programming in a couple of years . . . but don't you need to pass variables when you call the function. Such as runGame(variable,variable) etc.?


I've taken a long hiatus, but I still visit sometimes. Give me some time to answer any messages you post on my projects!

Offline

 

#6 2011-07-22 09:51:31

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Python Help

Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)

Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.

Last edited by Harakou (2011-07-22 09:55:13)


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#7 2011-07-24 10:33:54

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Python Help

Harakou wrote:

Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)

Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.

How can I define them as Globals?  I'm not familiar in the field of local/global variables.  I thought that they were all global unless they were in a

Code:

def function(variable):

thing.

Offline

 

#8 2011-07-24 10:43:40

msdosdude
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Python Help

...


http://i39.tinypic.com/2vkxppf.png
Freebie. Not a Scratch OS, a REAL OS you can boot to, by msdosdude.

Offline

 

#9 2011-07-24 11:05:01

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Python Help

maxskywalker wrote:

Harakou wrote:

Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)

Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.

How can I define them as Globals?  I'm not familiar in the field of local/global variables.  I thought that they were all global unless they were in a

Code:

def function(variable):

thing.

Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:

Code:

var1 = 42

def change(num):
     var1 = 42
     var1 += num

change(1337)
print(var1)

While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function.  What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.

Code:

var1 = 42

def change(num):
     global var1
     var1 = 42
     var1 += num

change(1337)
print(var1)

In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#10 2011-07-24 16:20:09

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Python Help

Harakou wrote:

maxskywalker wrote:

Harakou wrote:

Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)

Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.

How can I define them as Globals?  I'm not familiar in the field of local/global variables.  I thought that they were all global unless they were in a

Code:

def function(variable):

thing.

Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:

Code:

var1 = 42

def change(num):
     var1 = 42
     var1 += num

change(1337)
print(var1)

While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function.  What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.

Code:

var1 = 42

def change(num):
     global var1
     var1 = 42
     var1 += num

change(1337)
print(var1)

In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.

So does that mean that I can just say

Code:

global pp1 = *
global pp2 = *
global pp3 = *
global pp4 = *

?

Offline

 

#11 2011-07-24 16:30:18

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Python Help

maxskywalker wrote:

Harakou wrote:

maxskywalker wrote:


How can I define them as Globals?  I'm not familiar in the field of local/global variables.  I thought that they were all global unless they were in a

Code:

def function(variable):

thing.

Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:

Code:

var1 = 42

def change(num):
     var1 = 42
     var1 += num

change(1337)
print(var1)

While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function.  What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.

Code:

var1 = 42

def change(num):
     global var1
     var1 = 42
     var1 += num

change(1337)
print(var1)

In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.

So does that mean that I can just say

Code:

global pp1 = *
global pp2 = *
global pp3 = *
global pp4 = *

?

No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#12 2011-07-24 16:37:34

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Python Help

Harakou wrote:

maxskywalker wrote:

Harakou wrote:

Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:

Code:

var1 = 42

def change(num):
     var1 = 42
     var1 += num

change(1337)
print(var1)

While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function.  What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.

Code:

var1 = 42

def change(num):
     global var1
     var1 = 42
     var1 += num

change(1337)
print(var1)

In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.

So does that mean that I can just say

Code:

global pp1 = *
global pp2 = *
global pp3 = *
global pp4 = *

?

No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.

But how?  I tried doing 'global var' after every declaration, but it didn't work.

Last edited by maxskywalker (2011-07-24 16:38:36)

Offline

 

#13 2011-07-24 16:49:51

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Python Help

maxskywalker wrote:

Harakou wrote:

maxskywalker wrote:


So does that mean that I can just say

Code:

global pp1 = *
global pp2 = *
global pp3 = *
global pp4 = *

?

No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.

But how?  I tried doing 'global var' after every declaration, but it didn't work.

Adding this to the beginning of your playerMove() function (unless there's a variable I missed) should make that function work. Then just do the same with the other functions, except with whatever variables they need to work with to work.

Code:

global speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk

http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#14 2011-07-24 16:56:58

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Python Help

Harakou wrote:

maxskywalker wrote:

Harakou wrote:


No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.

But how?  I tried doing 'global var' after every declaration, but it didn't work.

Adding this to the beginning of your playerMove() function (unless there's a variable I missed) should make that function work. Then just do the same with the other functions, except with whatever variables they need to work with to work.

Code:

global speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk

Okay!  Thanks, man!  Doing now!

Offline

 

#15 2011-07-24 18:55:15

cheddargirl
Scratch Team
Registered: 2008-09-15
Posts: 1000+

Re: Python Help

Closed by request of owner.


http://i.imgur.com/8QRYx.png
Everything is better when you add a little cheddar, because when you have cheese your life is at ease  smile

Offline

 

#16 2011-12-18 16:50:15

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Python Help

Reopened by request of the topic owner.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#17 2011-12-19 10:20:01

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Python Help

Alright, now, new problem.

Offline

 

#18 2011-12-19 11:06:26

nickbrickmaster
Scratcher
Registered: 2010-02-02
Posts: 500+

Re: Python Help

You could try putting a checker in the runGame() function. Like

Code:

 
if loop == 0:
    break

Ask me what I'm doing, wait an hour than roll a die, if it's 4-6, I'm playing Skyrim, if it's 1, I'm eating, if it's 2-3 I'm programming.
Steam: nickbrickmaster | RotMG: PwnThemAll | Minecraft: nickbrickmaster | League Of Legends: BaneOfTitans

Offline

 

#19 2011-12-19 16:09:40

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Python Help

nickbrickmaster wrote:

You could try putting a checker in the runGame() function. Like

Code:

 
if loop == 0:
    break

The break function only works in a loop.  If I put it in the while loop, it doesn't work anyway.

Offline

 

#20 2011-12-20 10:24:23

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Offline

 

Board footer