简体   繁体   中英

Assigning a new value to a python variable

I'm extremely new to Python and working on my first text-based game. In this game, I would like player to choose from a list of characters. In the course of the game, I would also like to give the player the ability to change the character (which ultimately affects the outcome of the game). I'm unable to understand what I need to do to ensure the new choice of character is saved appropriately. Below is a stripped down version of the code:

def identity_choice():
    identity = input("""Your options are:
    1.  Ordinary Tourist
    2.  Chef
    Please choose a number""")
    if identity == "1":
        print("You now look like a tourist!")
        return identity
    elif identity == "2":
        print("You are now a chef")
        return identity
    else:
        print("Sorry, I don't understand that.")
        return identity_choice()

def action(identity):
    if identity == "1":
        print("You can't do what you need to as a tourist")
        response = input("Would you like to change your disguise?")
        if "y" in response:
            identity_choice()
        else:
            print("You have chosen to retain your identity")

identity = identity_choice()

action(identity)

The variable "identity" is used only local to the function(s). If you need the variable global, just declare the variable outside all functions, and inside the functions you enter the line "global identity".

I don't exactly understand what you mean. I assume that the problem is in the "action(identity)" function that the new identity is not saved in case the user chooses "y". Change it into:

def action(identity):
   if identity == "1":
      print("You can't do what you need to as a tourist")
      response = input("Would you like to change your disguise?")
   if "y" in response:
      return identity_choice()
   else:
      print("You have chosen to retain your identity")
      return identity

Then, when you call action, do it like this:

identity = action(identity)

Short Answer

All you need to do is reassign the identity variable, if this is the way you are choosing to keep track of the players identity. For example:

if "y" in response: identity = identity_choice()

After this you can return identity if you would like, or you may, as the other answer suggested, declare global identity inside the action function description. This way, the identity variable is shared throughout the entire script.

Longer Answer

Actually won't be that long, but this seems like a great place to learn some object oriented programming. You stated that you're just starting, so if it's too early, then don't bother just yet, you'll get there!

However, a nice way to set this up could be to instantiate users as an object of some class, such as user , and then this class could either have a second class identity which inherits from the user class. Or, the user class could simply have a variable called identity , which can be changed with a simple function. Here is a very brief and incomplete example:

class user:
    def __init__(self):
        pass   # here you could call the choose identity function, or just 
               # initialize with some default identity
    def choose_identity():
        #the function you wrote above asking the user to choose their identity

Hope this helps a bit, I tried to keep it very simple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM