简体   繁体   English

如何根据输入打印出正确的响应?

[英]How can I print out the correct response based on input?

I'm new to Python and I have a very simple problem that I'd like explained to me. 我是Python的新手,我有一个非常简单的问题,我想向我解释。 I'm making a text adventure game and I'd like to be able to call another function inside a function so my code is organized better. 我正在制作一个文字冒险游戏,我希望能够在一个函数内调用另一个函数,以便更好地组织我的代码。

This is what I have that's working: 这是我正在工作的:

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() 

But I'd like it to look something like this, but I don't know how to make it work: 但我希望它看起来像这样,但我不知道如何使其工作:

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() 

You're not passing the return value of choosePath() to checkPath() . 您没有将choosePath()的返回值传递给checkPath() It should be: 它应该是:

path = choosePath()
checkPath(path)

Or as a one-liner: 还是单线:

checkPath(choosePath())

You have to pass the return value of choosePath to checkPath . 您必须将choosePath的返回值choosePathcheckPath

checkPath(choosePath())

checkPath takes a parameter, so you need to supply a value. checkPath需要一个参数,因此您需要提供一个值。 That value is the return value of choosePath , either "LEFT" or "RIGHT". 该值是choosePath的返回值,即“ LEFT”或“ RIGHT”。 Once you pass it in, the checkPath function will execute, checking the string and printing accordingly. 传递checkPath将执行checkPath函数,检查字符串并进行相应打印。 Replace the choosePath and checkPath call with the above code. 用上面的代码替换choosePathcheckPath调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM