Function not defined?

Function not defined?

Hi. I've never used these forums, but I couldn't find an answer to my question in other places. I've been using the codecademy python track, and have been making my own little game. I made a function, prologue, and that works fine. I made another one, ActionChoice, that worked, but once I added a few things to the function, it says it's not defined. Please help, I can't figure out what the problem is.

Code: Select all
`#New Game, with variables, and better choices.Money = 500Wall_Health = 250Wall_Rate = 25Tax = 50def prologue():    print "Welcome to A Tale of Kings, a sequel to A Tale of Things. This game is not like AToT, because you are not just choosing little tiny paths. Your choices carry on, and you deal with money, foreign relations, and other troubles."    Name = raw_input("What did you say your name was?")    print "Ah right. You are the good Sir " + Name + ", One of the members of a great council for a king. This king, who rules the small kingdom of Tarnor, is currently facing a dilemma. It appears that a larger kingdom by the name of Engon has taken a fancy to your Amethyst deposits. They have attacked!"    poop = raw_input("How much money do you think you have?")    print "You have a small amount of money to work with, 500 Amethyst Royals. 'Tis a pity that the Amethyst Royals are so easy to come by, they are worth practically nothing. You will have to be clever with your useage of these funds."    print    print "You also have a good sturdy wall made out of cardstock. It currently has 250 layers of cardstock, and you hope it will last. Unfortunately, every action that you do, the enemy attacks your wall for 25 damage. Work fast!"    print    print "You think that you might be able to survive this war if only you could get the assistance of one neighboring kingdom, Farlon. The problem is, the king of that nation has set a price, 25 Golden Dragons for his help (Equivalent to 1500 Amethyst Royals). You will have to see if you can raise the funds....)"    Start()prologue()def Start():    Action = raw_input("What would you like to do? Make sure to type in all caps. BUILD WALL, COLLECT TAXES, SHOP?")    if Action == 'BUILD WALL':        if Wall_Rate == 25:            print "You build up the wall 25 layers, but the enemy also attacks it for 25 damage. You might want to buy some building tools in the shop."            Start()        else:            Wall_Health = Wall_Health - 25 + Wall_Rate            print "You build your wall up some, just as your enemy attacks it. You heal the wall " + Wall_Rate + ". The Wall health is now " + Wall_Health            Start()    elif Action == "COLLECT TAXES":        Wall_Health = Wall_Health - 25        Money = Money + Tax        if Tax == 50:            print "You go out and collect 50 Amethyst Royals from your subjects. Your money is now " + Money + "Pretty good, but remember you can buy Tax collectors in the shop that will raise your tax rate. Also, the enemy attacked, so your wall's health is now " + Wall_Health            Start()                    else:            print "You collect taxes from your subjects. Your money is now " + Money + ". The enemy attacked, so your health is now " + Wall_Health            Start()    elif Action == "SHOP":        Wall_Health = Wall_Health - 25        print "Welcome to the shop!"        print "We have many choices for you. Which menu would you like to see? Building tools? Tax Collectors? Or Weapons?"            `
Withnothing

Posts: 4
Joined: Sun Aug 25, 2013 6:11 pm

Re: Function not defined?

There is no function named ActionChoice() in the code you have shown.

But you are calling the Start() function before it's defined.
You are also trying to modify global variables from your function, which won't work without using the global statement.
I would recommend not using globals at all, but defining them inside the function instead, or passing them as arguments to your function.

I'd say you're trying to make the game much too complicated for where your knowledge of the language currently is.
Learning about classes will make creating this kind of a game much simpler.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Re: Function not defined?

Oh, thank you. I still don't really understand why one function works but the other doesn't, but I'll learn more about classes. I was kinda just following what I saw other people do, I've never made a game that complicated. Mostly simple choose your own adventures.

Thanks for the help
Withnothing

Posts: 4
Joined: Sun Aug 25, 2013 6:11 pm

Re: Function not defined?

move prologue() to end of file.
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi

Posts: 112
Joined: Sun May 12, 2013 8:36 pm

Re: Function not defined?

Ok, so I moved the Start function above the prologue. This made the Wall_Health variable not work, so I made a global statement. Then it randomly thinks I'm trying to add a string and an integer? I don't know what the matter could possibly be here, the variable is a number.

Code: Select all
`#New Game, with variables, and better choices.Money = 500Wall_Health = 250 Wall_Rate = 255Tax = 50    def Start():    Action = raw_input("What would you like to do? Make sure to type in all caps. BUILD WALL, COLLECT TAXES, SHOP?")    if Action == 'BUILD WALL':        if Wall_Rate == 25:            print "You build up the wall 25 layers, but the enemy also attacks it for 25 damage. You might want to buy some building tools in the shop."            Start()        else:            global Wall_Health                        Wall_Health = Wall_Health -25 + Wall_Rate #This is the problem right here            print "You build your wall up some, just as your enemy attacks it. You heal the wall. The Wall health is now " + Wall_Health            Start()    elif Action == "COLLECT TAXES":        Wall_Health = Wall_Health - 25        Money = Money + Tax        if Tax == 50:            print "You go out and collect 50 Amethyst Royals from your subjects. Your money is now " + Money + "Pretty good, but remember you can buy Tax collectors in the shop that will raise your tax rate. Also, the enemy attacked, so your wall's health is now " + Wall_Health            Start()                    else:            print "You collect taxes from your subjects. Your money is now " + Money + ". The enemy attacked, so your health is now " + Wall_Health            Start()    elif Action == "SHOP":                Wall_Health= Wall_Health - 25        print "Welcome to the shop!"        print "We have many choices for you. Which menu would you like to see? Building tools? Tax Collectors? Or Weapons?"                def prologue():    Wall_Health = 250    print "Welcome to A Tale of Kings, a sequel to A Tale of Things. This game is not like AToT, because you are not just choosing little tiny paths. Your choices carry on, and you deal with money, foreign relations, and other troubles."    Name = raw_input("What did you say your name was?")    print "Ah right. You are the good Sir " + Name + ", One of the members of a great council for a king. This king, who rules the small kingdom of Tarnor, is currently facing a dilemma. It appears that a larger kingdom by the name of Engon has taken a fancy to your Amethyst deposits. They have attacked!"    poop = raw_input("How much money do you think you have?")    print "You have a small amount of money to work with, 500 Amethyst Royals. 'Tis a pity that the Amethyst Royals are so easy to come by, they are worth practically nothing. You will have to be clever with your useage of these funds."    print    print "You also have a good sturdy wall made out of cardstock. It currently has 250 layers of cardstock, and you hope it will last. Unfortunately, every action that you do, the enemy attacks your wall for 25 damage. Work fast!"    print    print "You think that you might be able to survive this war if only you could get the assistance of one neighboring kingdom, Farlon. The problem is, the king of that nation has set a price, 25 Golden Dragons for his help (Equivalent to 1500 Amethyst Royals). You will have to see if you can raise the funds....)"    Start()prologue()`

Sorry If I'm being a burden, it's just I can't find help elsewhere.
Withnothing

Posts: 4
Joined: Sun Aug 25, 2013 6:11 pm

Re: Function not defined?

The reason is global variable can't be change in function without calling them in.
Code: Select all
`global Wall_Rate`

but this is frown upon and not recommend.

here are some options
Code: Select all
`#New Game, with variables, and better choices.#dict are weak reference objectgamedatadict = {'Money':500, 'Wall Health':250, 'Wall Rate':25, 'Tax':50}# There also using class as a struct. This is a c way.#class Structure(object):#    pass#gamedatastruct = Structure()#gamedatastruct.Money = 500#gamedatastruct.Wall_Health = 250#gamedatastruct.Wall_Rate = 25#gamedatastruct.Tax = 50# There also class way not shown here.def prologue(gamedata):   print "Welcome to A Tale of Kings, a sequel to A Tale of Things. This game is not like AToT, because you are not just choosing little tiny paths. Your choices carry on, and you deal with money, foreign relations, and other troubles."   Name = raw_input("What did you say your name was?: ")   print "Ah right. You are the good Sir " + Name + ", One of the members of a great council for a king. This king, who rules the small kingdom of Tarnor, is currently facing a dilemma. It appears that a larger kingdom by the name of Engon has taken a fancy to your Amethyst deposits. They have attacked!"   poop = raw_input("How much money do you think you have?: ")   print "You have a small amount of money to work with, 500 Amethyst Royals. 'Tis a pity that the Amethyst Royals are so easy to come by, they are worth practically nothing. You will have to be clever with your useage of these funds."   print   print "You also have a good sturdy wall made out of cardstock. It currently has 250 layers of cardstock, and you hope it will last. Unfortunately, every action that you do, the enemy attacks your wall for 25 damage. Work fast!"   print   print "You think that you might be able to survive this war if only you could get the assistance of one neighboring kingdom, Farlon. The problem is, the king of that nation has set a price, 25 Golden Dragons for his help (Equivalent to 1500 Amethyst Royals). You will have to see if you can raise the funds....)"   Start(gamedata)# Start dict version#"""def Start(gamedata):   loop = True   while loop:      Action = raw_input("What would you like to do? Make sure to type in all caps. BUILD WALL, COLLECT TAXES, SHOP?\n>> ")      if Action == 'BUILD WALL':         if gamedata['Wall Rate'] == 25:            print "You build up the wall 25 layers, but the enemy also attacks it for 25 damage. You might want to buy some building tools in the shop."            #Start() this is wrong use while loop instead         else:            gamedata['Wall Health'] = gamedata['Wall Health'] - 25 + gamedata['Wall Rate']            print "You build your wall up some, just as your enemy attacks it. You heal the wall " + str(gamedata['Wall Rate']) + ". The Wall health is now " + str(gamedata['Wall Health'])            #Start()      elif Action == "COLLECT TAXES":         gamedata['Wall Health'] = gamedata['Wall Health'] - 25         gamedata['Money'] = gamedata['Money'] + gamedata['Tax']         if gamedata['Tax'] == 50:            print "You go out and collect 50 Amethyst Royals from your subjects. Your money is now " + str(gamedata['Money']) + "Pretty good, but remember you can buy Tax collectors in the shop that will raise your tax rate. Also, the enemy attacked, so your wall's health is now " + str(gamedata['Wall Health'])            #Start()                     else:            print "You collect taxes from your subjects. Your money is now " + str(gamedata['Money']) + ". The enemy attacked, so your health is now " + str(gamedata['Wall Health'])            #Start()      elif Action == "SHOP":         gamedata['Wall Health'] = gamedata['Wall Health'] - 25         print "Welcome to the shop!"         print "We have many choices for you. Which menu would you like to see? Building tools? Tax Collectors? Or Weapons?"               elif Action.lower() in ['exit','quit','q']:         loop = False#"""         # start class version. c way"""         def Start(gamedata):   loop = True   while loop:      Action = raw_input("What would you like to do? Make sure to type in all caps. BUILD WALL, COLLECT TAXES, SHOP?")      if Action == 'BUILD WALL':         if gamedata.Wall_Rate == 25:            print "You build up the wall 25 layers, but the enemy also attacks it for 25 damage. You might want to buy some building tools in the shop."         else:            gamedata.Wall_Health = gamedata.Wall_Health - 25 + gamedata.Wall_Rate            print "You build your wall up some, just as your enemy attacks it. You heal the wall " + str(gamedata['Wall Rate']) + ". The Wall health is now " + str(gamedata.Wall_Health)      elif Action == "COLLECT TAXES":         gamedata.Wall_Health = gamedata.Wall_Health - 25         gamedata.Money = gamedata.Money + gamedata.Tax         if gamedata.Tax == 50:            print "You go out and collect 50 Amethyst Royals from your subjects. Your money is now " + str(gamedata['Money']) + "Pretty good, but remember you can buy Tax collectors in the shop that will raise your tax rate. Also, the enemy attacked, so your wall's health is now " + str(gamedata.Wall_Health)         else:            print "You collect taxes from your subjects. Your money is now " + str(gamedata.Money) + ". The enemy attacked, so your health is now " + str(gamedata.Wall_Health)      elif Action == "SHOP":         gamedata.Wall_Health = gamedata.Wall_Health - 25         print "Welcome to the shop!"         print "We have many choices for you. Which menu would you like to see? Building tools? Tax Collectors? Or Weapons?"               elif Action.lower() in ['exit','quit','q']:         loop = False"""      prologue(gamedatadict)`
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi

Posts: 112
Joined: Sun May 12, 2013 8:36 pm

Re: Function not defined?

Wow. Thanks for the help. I had no idea it would be that complex. I see I definitely need to read more on classes and the like. But thanks for showing me how I would go around doing that
Withnothing

Posts: 4
Joined: Sun Aug 25, 2013 6:11 pm