多个while和if语句的替代方案

huangapple go评论63阅读模式
英文:

ALternatives to multiple while and if statements

问题

这种情况下我还能以其他方式为用户提供多层级选项并检查用户在每个级别输入正确的值而不必使用多个while循环try和if语句

```python
while True:
    try:
        user_choice = int(input("输入选项 1 或 2"))
        if user_choice == 1 or user_choice == 2:
            if user_choice == 1:
                while True:
                    try:
                        edit_choice = int(input("您想编辑 (1) 还是删除 (2)"))
                        if edit_choice == 1 or edit_choice == 2:
                            if edit_choice == 1:
                                \# 提供更多选项和更多的while、try和if语句的代码。
                                break
                            elif edit_choice == 2:
                                \# 提供更多选项和更多的while、try和if语句的代码。
                                break
                        else:
                            print("请输入有效的选项。")
                    except ValueError:
                        print("请输入数字。")
                    break
            elif user_choice == 2:
                \# 提供更多选项和更多的while、try和if语句的代码。
                break
        else:
            print("请输入有效的选项。")
    except ValueError:
        print("请输入数字。")
英文:

The state of this. How else can I offer the user multiple levels of options and check that the user inputs the correct value at each level without having to use multiple while loops, try and if statements.

while True:
    try:
        user_choice = int(input("Enter an option 1 or 2"))
        if user_choice == 1 or 2:
            if user_choice = 1:
                while True:
                    try:
                        edit_choice = int(input("Would you like to edit (1) or delete (2)"))
                        if edit_choice == 1 or 2:
                            if edit_choice = 1:
                                \# code to offer more options and more while, try and if statements.
                                break
                            elif edit_choice = 2:
                                \# code to offer more options and more while, try and if statements.
                                break
                        else:
                            print("\\nPlease enter a valid option.")
                    except ValueError:
                        print("\\nPlease enter a number.")
                    break
            elif user_choice = 2:
                \# code to offer more options and more while, try and if statements.
                break
        else:
            print("\\nPlease enter a valid option.")
    except ValueError:
        print("\\nPlease enter a number.")

答案1

得分: 1

def enter_option():
    try:
        user_choice = int(input("输入选项 1 或 2:"))
        if user_choice in [1, 2]:
            if user_choice == 1:
                edit_or_delete()
            elif user_choice == 2:
                edit_or_delete()
        else:
            print("\n请输入一个有效选项。")
            enter_option()
    except ValueError:
        print("\n请输入一个数字。")
        enter_option()


def edit_or_delete():
    try:
        edit_choice = int(input("你想编辑 (1) 还是删除 (2):"))
        if edit_choice in [1, 2]:
            if edit_choice == 1:
                enter_option() # 提供更多选项的代码
            elif edit_choice == 2:
                enter_option() # 提供更多选项的代码
        else:
            print("\n请输入一个有效选项。")
            edit_or_delete()
    except ValueError:
        print("\n请输入一个数字。")
        edit_or_delete()


enter_option()
英文:

You rewrite it using recursive function calls instead of while loops like this:

def enter_option():
    try:
        user_choice = int(input("Enter an option 1 or 2: "))
        if user_choice in [1, 2]:
            if user_choice == 1:
                edit_or_delete()
            elif user_choice == 2:
                edit_or_delete()
        else:
            print("\nPlease enter a valid option.")
            enter_option()
    except ValueError:
        print("\nPlease enter a number.")
        enter_option()


def edit_or_delete():
    try:
        edit_choice = int(input("Would you like to edit (1) or delete (2): "))
        if edit_choice in [1, 2]:
            if edit_choice == 1:
                enter_option() # code to offer more options
            elif edit_choice == 2:
                enter_option() # code to offer more options
        else:
            print("\nPlease enter a valid option.")
            edit_or_delete()
    except ValueError:
        print("\nPlease enter a number.")
        edit_or_delete()


enter_option()

huangapple
  • 本文由 发表于 2023年7月3日 05:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定