1
def Change_char_stats():
    Char_dmg = 50
    Char_health = 100
    Char_stat_choice= ''
    print('Current Damage is:',Char_dmg,'and health is:',Char_health,'.')
    Char_stat_choice=input('\nWhat character stat would you like to edit?')

    if Char_stat_choice == '1':
        print('Current damage is',Char_dmg,'.')
        Char_dmg=int(input('Character damage to: '))
        print('Character damage has been changed to',Char_dmg,'.')
        Change_char_stats()

    elif Char_stat_choice == '2':
        print('Current damage is',Char_health,'.')
        Char_health=int(input('Character health to: '))
        print('Character health has been changed to',Char_health,'.')
        Change_char_stats()
    else:
        print('Input invalid.')
        Change_char_stats()

Change_char_stats()

So basically I'm working on a simple game for myself on Python, and I'm having an issue with my variables as when I run the program original variables are set to 50 dmg and 100 health, but what I want to do is be able to run the code, change the variables and then have them stay as that. Although I understand why the variables aren't staying as the new values, I have no clue how to over-write them, help would be much appreciated.

Thanks.

4
  • You are missing a closing quote ' on this line Char_stat_choice=input('\nWhat character stat would you like to edit?) Commented Feb 13, 2017 at 0:10
  • 2
    You keep calling Change_char_stats() recursively but you don't keep track of any changes done to Char_health etc. It might be better to have a character represented as an instance of a class so that you can keep track of changes. Commented Feb 13, 2017 at 0:13
  • 1
    You should be usign a class and use instances there - but you really should understand better how funcitons workm before defining yur character class. I suggest you take read on the Python tutorial at docs.python.org/3/tutorial/index.html Commented Feb 13, 2017 at 0:24
  • Do the if statements belong inside the Change_char_stats() function? Commented Feb 13, 2017 at 0:25

2 Answers 2

2

I suggest creating a class to package all of the variables into a single object:

def class player_character:
    def __init__(self):
        self.health = 100
        self.dmg = 50

Now you create an instance of the class:

player = player_character()

And change the variables directly:

player.health -= 10

Alternatively, you can add functions to the class:

def class player_character:
    def __init__(self):
        self.health = 100
        self.dmg = 50

    def hit(self, dmg):
        self.health -= dmg

Now you can call the function on an object:

player.hit(10)

Classes are incredibly powerful and great tools for organizing code. They allow you to treat a lot of data as a single entity. I strongly encourage you to learn more about them and object oriented programming in general.

Sign up to request clarification or add additional context in comments.

Comments

0

Place the variables outside function body and make them accessible within usng global keyword:

somevar = 5

def foobar(x):
    global somevar
    somevar = x

print somevar
foobar(6)
print somevar

2 Comments

This would be interesting in a multiplayer game :) There's no reason to use globals here.
Well, there is in a sense it does properly answer what the OP asked. I would not downvote this answer, as it is "correct". But I won't upvote it either, unless it explains what are the issues with this design, and offers a better one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.