You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python程序中实现goagain提问的循环校验而非重启整个函数?

问题解决:仅重启继续提问环节而非整个函数

需求与问题

我要编写一个Python程序,逻辑如下:

  • 询问姓名、年龄和性别
  • 输出包含这些信息的语句
  • 若语句正确,询问是否继续(goagain)
  • 选择“yes”则重启信息录入函数,选择“no”则退出程序
  • 若goagain输入非yes/no,提示用户后重新询问该问题

目前遇到的问题:不知道如何仅重启goagain提问环节而非整个函数,是否需要单独编写goagain函数或使用while循环?

原代码

#Take the input
def infofun():
    name = input("Please enter your name: ")
    age = input("Please enter your age: ")
    gender = input("Please enter your gender: ")
    gender = gender.lower()
    #Determine the output based on gender selection
    if gender == "male":
        ans = input(f"Your name is {name}, you are {age} years old and your are a man? (Yes/No) ")
        ans = ans.lower()
        # If answer is correct, ask if they want to go again
        if ans == "yes":
            goagain = input("I got it right! Want to go again? ")
            goagain = goagain.lower()
            # If go again is yes, rerun the function
            if goagain == "yes":
                infofun()
            # If go again is no, quit the program
            elif goagain == "no":
                print("Goodbye!")
            # Else print invalid input and restart the go again question. This is where I am lost
            else:
                print("Invalid Input. Please enter yes or no")
        # If answer is not correct, restart the function
        elif ans == "no":
            print("Let's try again")
            infofun()
        # Same as before. If input is invalid, restart the go again question
        else:
            print("Invalid Input. Please enter yes or no")
    # This is a continuation of the initial question. Same steps apply.
    elif gender == "female":
        ans = input(f"Your name is {name}, you are {age} years old and your are a woman? (Yes/No) ")
        ans = ans.lower()
        if ans == "yes":
            goagain = input("I got it right! Want to go again? ")
            goagain = goagain.lower()
            if goagain == "yes":
                infofun()
            elif goagain == "no":
                print("Goodbye!")
            else:
                print("Invalid Input. Please enter yes or no")
        elif ans == "no":
            print("Let's try again")
            infofun()
        else:
            print("Invalid Input. Please enter yes or no")
    else:
        print("Invalid input. Please enter yes or no")

infofun()

解决方案

用while循环是最直接的方式,既能单独处理重复提问环节,又能减少代码冗余。核心是把需要重复的提问逻辑放在循环里,直到用户输入有效选项为止。

修改后的代码

def infofun():
    # 录入用户基础信息
    name = input("Please enter your name: ")
    age = input("Please enter your age: ")
    gender = input("Please enter your gender: ").lower()
    
    # 统一处理性别描述,避免重复代码
    if gender == "male":
        gender_desc = "man"
    elif gender == "female":
        gender_desc = "woman"
    else:
        print("Invalid input. Please enter male or female.")
        infofun()
        return
    
    # 循环确认信息,直到输入yes/no
    while True:
        ans = input(f"Your name is {name}, you are {age} years old and you are a {gender_desc}? (Yes/No) ").lower()
        if ans == "yes":
            # 循环处理继续提问,仅重复当前环节直到输入有效选项
            while True:
                goagain = input("I got it right! Want to go again? ").lower()
                if goagain == "yes":
                    infofun()
                    return
                elif goagain == "no":
                    print("Goodbye!")
                    return
                else:
                    print("Invalid Input. Please enter yes or no")
        elif ans == "no":
            print("Let's try again")
            infofun()
            return
        else:
            print("Invalid Input. Please enter yes or no")

infofun()

关键修改点

  1. 抽离重复逻辑:把性别对应的描述(man/woman)统一处理,避免两段几乎一样的代码
  2. 用while循环处理重复提问:不管是确认信息的ans还是继续的goagain,都用while True循环包裹,输入无效时仅重复当前提问,不会回到信息录入步骤
  3. 添加return控制流程:在递归调用或退出前添加return,避免函数执行多余代码

内容的提问来源于stack exchange,提问作者user23498718

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:44:55