简体   繁体   中英

python - passing variables between functions

I am having trouble passing this dictionary between one function and another.

def player_main():
    player = {"health" : 100,
              "xp" : 100,
              "strength" : 0,
              "dexterity" : 0,
              "wisdom" : 0,
              "level_req" : None,
              "health_spawn" : None,
              "enemy_hit" : None}
    choice = difficulty_sequence()
    if choice == "easy":
        player = easy_difficulty(player)
    elif choice == "hard":
        player = hard_difficulty(player)
    player = character_creation(player)
    print(player)
    return player

In theory my second function should call 'player' from the first. The second isn't altering the values of it at all, but several of its variables depend on the first function.

def enemy_flail():
    print("Flail!")
    player = player_main()
    stat = player["strength"]
    print(stat)
    damage = stat//4 + 5
    print(damage)
    return damage

Can anyone see where I am going wrong?

Edit - enemy_flail() won't print either stat or damage, which leads me to believe it isn't calling player_main() in the first place.

每次调用enemy_flail您都在创建一个新的player词典,重置所有值。

您可能需要在player = player_main() enemy_flail函数之外定义player = player_main() ,并且只操作一次,然后每次都调用enemy_flail(player)

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