正如你可能已经从这个话题中找到的那样,我非常棒。无论如何,我在从另一个函数内部调用函数时遇到问题,因为Python似乎忽略了调用。
这是我的函数if
中的y
:
if clas == "warrior":
strenght, toughness = warrior()
print "Your strenght is %d." % strenght
print "Your toughness is %d." % toughness
return strenght, toughness
warrior_journey()
我在这里使用return将这些值分配给我的y
功能块之外的全局变量:
char_str, char_tough = character()
现在我的y
函数调用warrior()
没有任何问题,但忽略warrior_journey()
,只打印strenght, toughness
然后结束。它可能与return
有关,但我找不到任何有用的东西。
这是整个" y"或"字符"功能代码:
def character():
name = raw_input("Welcome, please give the name of your character:\n> ")
if name.isalpha() == True and len(name) != 0 and len(name) != 1:
print "Welcome again %s!" % name
else:
exit("This isn't a proper name")
clas = raw_input("Great, now choose your class (warrior, rogue, thief):\n> ").lower()
if "warrior" in clas or "rogue" in clas or "thief" in clas:
print "Your class is %s." % clas
else:
exit("You have only three choices listed above, read them again")
if clas == "warrior":
strenght, toughness = warrior()
print "Your strenght is %d." % strenght
print "Your toughness is %d." % toughness
warrior_journey()
return strenght, toughness
elif clas == "rogue":
strenght, toughness = rogue()
print "Your strenght is %d, and your toughness is %d." % (strenght, toughness)
return strenght, toughness
elif clas == "thief":
strenght, toughness = thief()
print "Your strenght is %d, and your toughness is %d." % (strenght, toughness)
return strenght, toughness
然后,ouside,我得到了:
char_str, char_though = character()
甚至还有问题的功能warrior_journey:
def warrior_journey():
print "You wake hangovered in the woods, sun rays dancing on your face"
print "as you regain parts of your consciousness you notice that, happily, your weapon is still at your side"
print "You have an open world before you, in which direction do you stumble (north, south, east, west)?"
direction = raw_input("> ").lower()
if direction == "north" or direction == "n":
warrior_north()
elif direction == "south" or direction == "s":
warrior_south()
elif direction == "east" or direction == "e":
warrior_east()
elif direction == "west" or direction == "w":
warrior_west()
else:
print "This is not a valid direction, perhaps you should sleep some more."
它还在' wip'因此,warrior_journey不会返回任何内容或发送给另一个函数,但它仍然至少应该通过#34;字符"功能,不应该吗?
答案 0 :(得分:3)
您在之前return
对warrior_journey()
进行函数调用。一旦函数return
,它就会停止执行该函数,因此您永远不会调用warrior_journey()
。尝试
warrior_journey()
return strenght, toughness
答案 1 :(得分:1)
return
从函数返回,表示其执行已停止。您必须将return
语句放在函数的末尾。
答案 2 :(得分:1)
你在return语句后写了一个语句。所以这意味着它会被忽略,因为Python会立即返回你的全局变量。在同一函数中返回语句后的语句始终不可访问。将函数调用warrior_journey()移动一个低于全局变量的语句,因此该变量在调用warrior_journey()之前得到返回的值。
答案 3 :(得分:1)
如前所述,'return'返回调用者,停止执行该函数。
我补充一点,您可以通过调试方法检查此问题和其他问题。
最简单(也是最难)的方法是在代码中添加'print'文件以突出显示其流程:
print 1
if clas == "warrior":
print 2
strenght, toughness = warrior()
print 3
print "Your strenght is %d." % strenght
print 4
print "Your toughness is %d." % toughness
print 5
return strenght, toughness
print 6
warrior_journey()
print 7
如果您运行这段代码,您会看到编号将在返回之前停在5 ...
1
2
3
4
5
下一个调试步骤可以是pdb,然后是IDE中嵌入的东西(例如Eclipse上的PyDev)
答案 4 :(得分:1)
正如其他人提到的,return
正在warrior_journey()
来电之前停止执行。如果您收到NameError: global name "warrior_journey" is not defined
,则需要查看warrior_journey
函数的定义。
至于其他错误。
char_str, char_tough = character()
这不会为全局变量赋值,而是为名为相同的局部变量赋值。要从函数内部设置全局变量,您需要使用global
关键字。
global char_str, char_tough
char_str, char_tough = character()