0

I've figured out two ways of assigning global variables.

The first method assigns an attribute to a function.

The second method changes a global variable name.

I will be implementing this into a text-based adventure game.

What is the difference between these methods. When is each method commonly used. What method is suitable for my task?

Here is the code.

# Method 1. Assigning attributes to function.

def coin():
    print "You see a coin. Pick it up?"

    choice = raw_input("> ")

    if choice == "yes":
        coin.amount = coin.amount + 1
        print coin.amount
    elif choice == "no":
        print "No monies for you."


# Method 2. Assigning global name within function. 

def coin2():
    global purse
    print "You see a coin. Pick it up?"

    choice = raw_input("> ")

    if choice == "yes":
        purse = purse + 1
        print purse
    elif choice == "no":
        print "No monies for you."

coin.amount = 0
coin()

purse = 0
coin2() 

Note: Previous answers I've discovered have seem to broad, so I will ask a new question specific to my problem of deciding which is most suitable for a text-based adventure game.

user3200293
  • 181
  • 5
  • 18
  • Previous answers I've discovered have seem to broad, so I will ask a new question specific to my problem of deciding which is most suitable for a text-based adventure game. @martijn Pieters, would you be able to advise since you've stated the answer already exists? – user3200293 Sep 18 '15 at 10:25
  • You were asking several questions in the post; try to stick to *one* question per post. The duplicate I linked to covers the same subject, which should answer a large proportion of your questions, but note that I closed that as Too Broad as well. This isn't a game, we are trying to build a repository of quality questions with answers, with lasting value for future visitors too. – Martijn Pieters Sep 18 '15 at 10:27
  • I appreciate that I may not know the etiquette of stackoverflow being a beginner to both coding and using this site, so with that in mind it's probably best to give respectful replies, which will help beginners with your goal of building a repository of quality questions, rather than snidey remarks that berate and patronise them. I apologise whole heartedly for clearly treating stack-overflow as a game, as opposed to genuinely seeking advice. – user3200293 Sep 18 '15 at 10:33
  • I'm sorry you feel my replies were snidey, they were not intended as such. We get *thousands* of new questions each day, many from new users, and we cannot give feedback to all of those. This is why we have the tour and the help center, to try and educate as early as possible about how this site works. – Martijn Pieters Sep 18 '15 at 10:36
  • 1
    No to worry. I will follow your advise and re-post it with one question per post. Thank you. – user3200293 Sep 18 '15 at 10:38

0 Answers0