7 Replies - 5150 Views - Last Post: 04 September 2011 - 04:23 PM Rate Topic: -----

#1 nicse4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-August 11

Making a text-based RPG in python

Posted 17 August 2011 - 03:23 AM

I am working on a text-based RPG, and am having a few issues, can you help me?
global maxHP
global HP
global maxMP
global MP
global gold
global loop1
gold = 0
def start():
    print "An evil dragon known as Draco has taken control of the world.\nHe has a big army of minions who do his bidding and his dirty work.\nYou must rise and defeat him and all his minions."
    choice = raw_input("one is the choice")
    if choice == 1:
        dragoncave()
    else:
        dragoncave()
def dragoncave():
    while gold != 100:
        print "YOU NEED 100 GOLD TOO LEAVE!"
        print "You found 100 gold!"
        gold = 100
        loop1 = 1
    print "YOU WON!"

loop1 = 0
while loop1 == 0:
    start()

It says there is an error when going to the dragoncave, and that gold was used before it was defined. What is wrong? I have tried to define gold again, but it didn't work.

Is This A Good Question/Topic? 0
  • +

Replies To: Making a text-based RPG in python

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Making a text-based RPG in python

Posted 17 August 2011 - 07:25 AM

You need to declare gold as being a global:

def dragoncave():
    global gold
    ...


Was This Post Helpful? 0
  • +
  • -

#3 nicse4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-August 11

Re: Making a text-based RPG in python

Posted 17 August 2011 - 08:56 AM

Oh so define gold in the dragons cave? but what about when i need it in the other parts later in development?
also I did it like this

global gold
#other codes
def dragoncave():
#rest of code


So why doesn't this work?

This post has been edited by nicse4: 17 August 2011 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Making a text-based RPG in python

Posted 17 August 2011 - 09:01 AM

What you are doing is telling the interpreter that it should look for the variables in higher scopes; this allows you to define gold globally in for some functions and locally for others. If it was done your way, you would never be able to define gold as a local variable inside a function.

It may seem that they are quite similar, and your way is easier to use, but imagine if you declared the variable i in the global scope, then imported some other library: if that library ever used i to iterate over a list or range, you would lose what you were storing there.
Was This Post Helpful? 0
  • +
  • -

#5 nicse4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-August 11

Re: Making a text-based RPG in python

Posted 17 August 2011 - 09:05 AM

OK, I get it now, thank you so much.
Was This Post Helpful? 0
  • +
  • -

#6 Wigglesniff  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 17-August 11

Re: Making a text-based RPG in python

Posted 03 September 2011 - 05:38 AM

How much more have you done with the program?
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Making a text-based RPG in python

Posted 04 September 2011 - 03:24 PM

Could you clarify your question?
Was This Post Helpful? 0
  • +
  • -

#8 Wigglesniff  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 17-August 11

Re: Making a text-based RPG in python

Posted 04 September 2011 - 04:23 PM

I was wondering if (s)he had worked on the program anymore.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1