我尝试创建一个变量,可以在其中添加多个条件,以便在while语句中使用它
conditionData = "False"
def conditionConstructor(xStart, xEnd, yStart, yEnd, conditionData):
conditionData += " or (({} < x < {}) and ({} < y < {}))".format(xStart, xEnd, yStart, yEnd)
return conditionData
conditionConstructor(1, 2, 3, 4, conditionData)
conditionConstructor(3, 4, 1, 2, conditionData)
while(conditionData):
...
最好的方法是什么?也许有一种不用绳子的方法
您可以使用一系列
lambda
来获得您想要的弦从来就不是一种方式
输出:
要使用字符串作为代码,需要小心地使用
eval
函数,因为如果依赖用户的输入,它不是一个安全的函数。 注意,f字符串是一种改进的format
打印第一个
res
将显示:False or ((1 < x < 3) and (3 < y < 5))
打印第二个res将显示:
False or ((1 < x < 3) and (3 < y < 5)) or ((3 < x < 4) and (1 < y < 2))
相关问题 更多 >
编程相关推荐