4

I am trying to write a code to display atribute of the hero after the entry. But they all return 0 at the output

#This is one of code block that i use to calculate damage of heroes in Dota 2

def hero_attribute_input(hero_str,hero_agi,hero_int):
        print "Input the str of the hero"
        hero_str = int(raw_input("> "))
        print "Input the agi of the hero"
        hero_agi = int(raw_input("> "))
        print "Input the int of the hero"
        hero_int = int(raw_input("> ")) # Why can't you add a print commat here?
        return (hero_str,hero_agi, hero_int)

hero_str = 0
hero_agi = 0
hero_int = 0

hero_attribute_input(hero_str,hero_agi,hero_int)
#Why don't the variables change after this?

print hero_str
print hero_agi
print hero_int
poolie
  • 9,289
  • 1
  • 47
  • 74
An Pham
  • 53
  • 1
  • 1
  • 6
  • 2
    Because you need to do `global hero_stro` in `def hero_attribute_input` at the very first line. Otherwise it's a variable bound to the function scope. – Torxed Nov 18 '15 at 17:13

2 Answers2

4

The reason why it isn't working, is because if you don't define a variable as global within your function, it will not affect "outside" variables.

A good explanation can be found here: Short Description of the Scoping Rules?

Try adding global hero_str to the function and that should solve the issue.

hero_str = 0
hero_agi = 0
hero_int = 0

def hero_attribute_input(hero_str,hero_agi,hero_int):
    global hero_str
    global hero_agi
    global hero_int
    print "Input the str of the hero"
    hero_str = int(raw_input("> "))
    print "Input the agi of the hero"
    hero_agi = int(raw_input("> "))
    print "Input the int of the hero"
    hero_int = int(raw_input("> ")) # Why can't you add a print command at here?
    return (hero_str,hero_agi, hero_int)


hero_attribute_input(hero_str,hero_agi,hero_int)

print hero_str
print hero_agi
print hero_int

Or catch the returning values. I noticed Bhargav Rao beat me to it, So i won't go through that in this answer.

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
3

Though you can make it global, it's not a good practise. Rather do this:

hero_str,hero_agi,hero_int = hero_attribute_input(hero_str,hero_agi,hero_int)

To explain this, you are updating the variables, but that won't affect the variables outside function scope.

So, you need to reassign the variables with the updated value what you are returning from your function.

return (hero_str,hero_agi, hero_int)

So, the full code should be more or less like below:

def hero_attribute_input(hero_str,hero_agi,hero_int):
    print "Input the str of the hero"
    hero_str = int(raw_input("> "))
    print "Input the agi of the hero"
    hero_agi = int(raw_input("> "))
    print "Input the int of the hero"
    hero_int = int(raw_input("> "))
    return (hero_str, hero_agi, hero_int)

hero_str = 0
hero_agi = 0
hero_int = 0
hero_str, hero_agi, hero_int = hero_attribute_input(hero_str,hero_agi,hero_int)
print hero_str
print hero_agi
print hero_int

N.B. This is a run code on python 2.7 and successfully running without an error. If you still facing problem, then the problem lies in somewhere else.

Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • It said hero_str is not defined. I tried both global variable and value assigned method, but it said hero_str is not define, i guess it cause error at the beginning of the class – An Pham Nov 18 '15 at 17:40
  • @AnPham, I see no class here. The code you provided should not produce the error you mentioned. – Ahsanul Haque Nov 18 '15 at 17:44
  • when i input the first variable, it returned an error. File "dota2 calculating dmg.py", line 29, in hero_attribute_input(hero_str,hero_agi,hero_int) NameError: name 'hero_str' is not defined – An Pham Nov 18 '15 at 17:59
  • @AnPham, see the updated answer. As I mentioned in the answer too, I run this code on my machine with python 2.x and it's running fine. As you used `raw_input`, I guess you are using python 2.x too. So, there is no chance of facing such error. – Ahsanul Haque Nov 18 '15 at 18:10
  • oh Im so sorry, my bad. I ran the wrong .py file. I'm running it through vim. sr. Thanks anyway, it works well. – An Pham Nov 18 '15 at 18:13