简体   繁体   中英

why can't I print variables declared inside a function?

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

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.

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

NB 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.

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