0

This is a bit of code from my text based game:

location_now = ""

class what_do:

    def whaat(self):
        interactions = what_do()
        print "*What will you do? Type help_ to see all the actions*"
        what = raw_input("")
        if what == "help_":
            print ' '.join(help_)
            interactions.whaat()
        if what == "travel":
            print "These are all the cities you can travel to:"
            mapje.map()
            travel = raw_input("To which city do you want to travel?(Takes 10 seconds)")
            if travel == locations[0] or travel == locations[1] or travel == locations[2] or travel == locations[3] or travel == locations[4] or travel == locations[5] or travel == locations[6] or travel == locations[7] or travel == locations[8]:
                print "You are now travelling to %s" % travel
                time.sleep(10)
                print "You are now in %s!" % travel
                location_now = travel
            else:
                print "That is no location in Skyrim!"
                interactions.whaat()

I want the input from travel = raw_input etc. to be stored and saved in the variable location_now (That I created before and outside the class). I have to use that input later in my code.

This class will be repeated because it is a sort of 'What would you like to do next?', so if the input from the second time, what = raw_input(""), is asked, it must replace the earlier input stored in location_now = ""

tux3
  • 7,171
  • 6
  • 39
  • 51
Hugius
  • 380
  • 1
  • 5
  • 15

3 Answers3

1

I believe you are worried that whatever is stored in your location_now variable would be overwritten if you use raw_input() again. Luckily, that doesn't happen and if you have stored the result of a raw_input() in a variable, it remains intact.

Are you facing any problems that made you arrive to the conclusion?

silent_dev
  • 1,566
  • 3
  • 20
  • 45
1

I would move your location_now variable into the class "what_do" as a static variable. (Static class variables in Python)

Also as a helpful hint, this line

if travel == locations[0] or travel == locations[1] or travel == locations[2] or travel == locations[3] or travel == locations[4] or travel == locations[5] or travel == locations[6] or travel == locations[7] or travel == locations[8]:

can be reduced to

if travel in locations:

This will check if travel is in the list of locations. Just a little hint to simplify your code! Isn't Python beautiful?

Community
  • 1
  • 1
nivix zixer
  • 1,611
  • 1
  • 13
  • 19
0

In your whaat() function when you are trying to assign a new value to location_now you are actually creating a new local variable called location_now. You are not assigning a new value to the global location_now.

You need to declare location_now as global before your assignment so that you will actually assign a new value to the global variable.

global location_now
location_now = travel
newspire
  • 6,144
  • 2
  • 17
  • 12
  • Thanks for the help, but I fixed it on a different way: making a list of location_now and append travel to it. But thanks for the help, next time I will know what to do :) – Hugius Apr 10 '15 at 20:50