正如您可能已经从该主题中发现的那样,我很菜鸟。无论如何,我在从另一个函数内部调用一个函数时遇到问题,因为 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()
y
我在这里使用 return 将这些值分配给我的功能块之外的全局变量:
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
然后,在外面,我有:
char_str, char_though = character()
甚至进一步有问题的功能 Warriors_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 不会返回任何东西或发送到另一个函数,但它仍然应该至少由“字符”函数中的调用打印,不是吗?