我是Python的新手,我有一个非常简单的问题,我想向我解释一下。我正在制作一个文本冒险游戏,我希望能够在函数内部调用另一个函数,这样我的代码组织得更好。
这就是我的工作:
def displayIntro():
print("You come to a crossroads on your trip home.")
print ("You can go LEFT towards the DEMON FOREST or RIGHT to SKULL MOUNTAIN")
print()
def choosePath():
path = ""
while path != "LEFT" and path != "RIGHT":
path = input("Which path will you choose? LEFT or RIGHT? --> ")
if path == "RIGHT":
print("You take the path to SKULL MOUNTAIN.")
elif path == "LEFT":
print ("You take the path to the DEMON FOREST.")
displayIntro()
choosePath()
choosenPath()
但我希望它看起来像这样,但我不知道如何使它工作:
def displayIntro():
print("You come to a crossroads on your trip home.")
print ("You can go LEFT towards the DEMON FOREST or RIGHT to SKULL MOUNTAIN")
print()
def choosePath():
path = ""
while path != "LEFT" and path != "RIGHT":
path = input("Which path will you choose? LEFT or RIGHT? --> ")
return path
def checkPath(choosePath):
if choosePath == "RIGHT":
print("You take the path to SKULL MOUNTAIN.")
elif choosePath == "LEFT":
print("You take the path to the DEMON FOREST.")
displayIntro()
choosePath()
checkPath()
答案 0 :(得分:2)
您未将choosePath()
的返回值传递给checkPath()
。它应该是:
path = choosePath()
checkPath(path)
或者作为一个单行:
checkPath(choosePath())
答案 1 :(得分:1)
您必须将choosePath
的返回值传递给checkPath
。
checkPath(choosePath())
checkPath
接受一个参数,因此您需要提供一个值。该值是choosePath
的返回值,“LEFT”或“RIGHT”。传入后,checkPath
函数将执行,检查字符串并相应地打印。使用上面的代码替换choosePath
和checkPath
来电。