python-3.x - Writing optimized multiple conditional statements Or use any other way to check condition? in python
问题描述
What is the optimized way to write below conditional statements, i wanted to avoid the multiple if-elif statements
def test(val):
return False if val is 0 else True
def test_func(x=None, y=None):
if(x == "check" and y == "1" and test(0) == False):
print("Option 1")
elif(x == "check" and y == "2" and test(0) == False):
print("Option 2")
elif(x == "uncheck" and y == "1" and test(1) == True):
print("Option 3")
elif(x == "uncheck" and y == "2" and test(1) == True):
print("Option 4")
解决方案
我做了这个:
if x == "check" and test(0) == False:
if y == "1":
print("Option 1")
else:
print("Option 2")
elif x == "uncheck" and test(1) == true:
if y == "1":
print("Option 3")
else:
print("Option 4")
我不知道它是否有帮助,因为它没有缩短(如果没有,请给我更多关于您尝试做什么的背景信息,以便我可以尝试不同的方法),但我无法对其进行更多优化。希望这可以帮助。
-JLT
推荐阅读
- reactjs - 除非'--jsx'-React 否则不能使用 JSX
- woocommerce - REST API POST 请求无法发布数据
- python - 我对简单代码中的函数有疑问
- rust - 如何从函数中返回一个函数以进行参数注入?
- jpackage - windows上的jpackage使程序无声无息
- c# - 从基本 asp.net 身份用户创建继承用户或为每个角色实现不同的接口?
- c# - Unity/C# - 仅在 VSCode 中的编译器错误,而不是 Unity 编辑器或 Visual Studio
- java - 二进制搜索功能未实现
- c - 用于验证矩阵在 C 中对称的递归函数
- flutter - 从表格中获取单元格值 - 颤动