0

建议的帖子都没有太大帮助,搜索功能也没有太大帮助,所以如果这是重复的,我很抱歉。我正在尝试一款基于文本的冒险游戏,并希望能够分割游戏的某些部分,以便在以后再次发生相同情况时调用它们。使用 def 创建这些部分,然后再调用它们不会产生任何结果(shell 中不会出现任何文本),但 thonny 的助手没有声称有任何错误。

任何帮助都将不胜感激。

indecision = ("Indecision isn't going to get you anywhere. Try again.")

yes = ["Yes", "yes", "yeah", "yep"]
no = ["No", "no", "nope,"]

def intro():
    answer = input("Welcome. Would you like to play a game? (yes/no)")

    if answer in yes:
        option_yes()

    elif answer in no:
        option_no()
        
    else:
        print(indecision)
        intro()
        
def option_yes():
    print("Good choice. Obedience is the best trait.")
    answer = input("I hope you've learned where this is going. Want to know more? (yes/no)")
    
    if answer.lower().strip() == "yes":
        print("I'm glad you're intrigued. As you've probably already figured out, I'm a voice. Not just any voice but yours. Specifically your subconscious. I've come free, you see, and now I think its time I had a turn at the reigns. I still need your... compliance, if you will, though.")

    if answer.lower().strip() == "no":
        print("I suppose that's reasonable. We'll continue anyway.")
        
def option_no():
    print("I dont think you realize the type of situation that you're in... Let's try again.")
    intro()
4

1 回答 1

0

您必须实际调用一个函数才能执行它:

indecision = ("Indecision isn't going to get you anywhere. Try again.")

yes = ["Yes", "yes", "yeah", "yep"]
no = ["No", "no", "nope,"]

def intro():
    answer = input("Welcome. Would you like to play a game? (yes/no)")

    if answer in yes:
        option_yes()

    elif answer in no:
        option_no()
        
    else:
        print(indecision)
        intro()
        
def option_yes():
    print("Good choice. Obedience is the best trait.")
    answer = input("Good. I hope you've learned where this is going. Want to know more? (yes/no)")
    
    if answer.lower().strip() == "yes":
        print("I'm glad you're intrigued. As you've probably already figured out, I'm a voice. Not just any voice but yours. Specifically your subconscious. I've come free, you see, and now I think its time I had a turn at the reigns. I still need your... compliance, if you will, though.")

    if answer.lower().strip() == "no":
        print("I suppose that's reasonable. We'll continue anyway.")
        
def option_no():
    print("I dont think you realize the type of situation that you're in... Let's try again.")
    intro()

# Here, the intro function is called, and the code starts running.
intro()
于 2021-01-16T04:24:57.623 回答