基于文本的游戏使用def函数定义游戏的各个部分,但不会产生任何结果或错误

2024-04-29 09:36:54 发布

您现在位置:Python中文网/ 问答频道 /正文

所有建议的帖子都没有多大帮助,搜索功能也没有,所以如果这是重复的话,我很抱歉。我正在尝试一款基于文本的冒险游戏,希望能够将游戏的某些部分分割开来,以便在以后再次出现相同场景时调用它们。使用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()

1条回答
网友
1楼 · 发布于 2024-04-29 09:36:54

您必须实际调用函数才能执行该函数:

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

相关问题 更多 >