在一个函数中定义的值在另一个函数中不可用

时间:2018-05-31 14:24:53

标签: python function return global variable-assignment

好的,这是我的第一篇文章。我在这里搜索过,试图找到任何问题以供参考,如果我错过了我道歉。

所以我的女儿和我一起写游戏。我一直在研究一些基础编程,而她在做创意的时候。 我有一个函数,用户选择一个“类”,为某些方面分配基值并将它们作为变量返回。在其他帖子之后,我首先尝试使用返回功能,但这不起作用,所以我尝试了全局功能。这允许程序使用变量简单地在函数外部打印它,但是当另一个函数尝试使用变量时,它会在赋值之前作为非变量引用。

     import random
     import time
     #these were another attempt at finding a way to make the classes
     #available to other functions
     combat = 0
     strength = 0 
     agility = 0
     magic = 0
     intelligence = 0
     def chose_class():
         #I tried return "each characteristic" at the end, but it wouldn't 
         #even print out the returned value
         global strength
         global combat
         global agility
         global magic
         global intelligence
         health = 10
         print("Chose your class:")
         print("Warrior: 1")
         print("Wizard: 2")
         print("Philosopher: 3")
         print("Thief: 4")
         print("Help: 5")
         dog1 = int(input(":"))
         if dog1 ==1:
             strength = 7
             combat = 7
             agility = 5
             stealth = 3
             magic = 1
             intelligence = 1
         elif dog1 == 2:
             strength = 3
             combat = 1
             agility = 3
             stealth = 3
             magic = 8
             intelligence = 6
         elif dog1 == 3: 
             strength = 3
             combat = 1
             agility = 3
             stealth = 3
             magic = 2
             intelligence = 8
         elif dog1 == 4:
             strength = 2
             combat = 4
             agility = 6
             stealth = 8 
             magic = 1
             intelligence = 6
         elif dog1 == 5:
             print("Warrior Dog:")
             print("    Warrior Dogs possess great strength and combat                                    ability")
             print("    they have medium agility, but low stealth, magic and")
             print("    intelligence.")
             print("\n"*2)
             print("Wizard Dog:")
             print("    Wizard Dogs have low strength, combat, and agility,")
             print("    they have medium stealth and high magic capabilities.")
             print("\n"*2)
             print("Philosopher Dog:")
             print("    Philosopher Dogs are highly intelligent, while they")
             print("    have low strength, they have medium combat ability.")
             print("    Their agility, stealth, and magic start low but can")
             print(" rapidly improve by finding hidden objects on quests.")  
             print("\n"*2)
             print("Thief Dogs")
             print("    Thief Dogs have high agility and stealth, as well as")
             print("    average intelligence and combat. They have low combat,")
             print("    magic, and strength.")
         else:
             chose_class()
     def room17():
         print("**Description**")
         print("Do you want to fight the cat guard or sneak past it?")
         print("Fight: 1")
         print("Sneak Past: 2")
         choice17 = str(input(":"))
         if "1" in choice17:
             fight17 = random.randint(0,5)
             combat17 = combat * fight17
             if combat17 <= 20:
                 print("You were successful")
                 combat = combat + 1
             else:
                 print("You lost the fight and had to retreat")
                 health = health - 1
                 room1()
         else:
             sneak17 = random.randint(0,5)
             stealth17 = sneak17 * stealth
             if stealth17 <= 20:
                 print("You snuck past the guard")
                 stealth = stealth + 1
             else:
                 print("The guard caught you, he attacks and you have to retreat")
                 health = health - 2
                 stealth = stealth - 1
                 room1()
     chose_class()
     # this was just to see if the value was returned
     print("Combat:", combat)
     # I'm jumping to the first combat event to try to test the program
     room17()

好的,这应该足以证明我的问题。我完全是自学成才,所以我确信有很多风格问题,我很乐意得到帮助。但是,如果有人可以帮助解决为什么变量“战斗”从“def chosen_class()”返回到打印命令但是然后没有在“def room17()”中分配,我会非常感激。

0 个答案:

没有答案