英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论