小编Den*_*kan的帖子

许多 if 语句的替代方案?

所以我有3个不同的变量。

model 是一个字符串并定义了 iPhone 模型。

storage 是一个定义电话存储的整数。

最后是价格,它是一个定义手机价格的整数。

例子:

model = iPhone 7
storage = 64 (GB)
price = 700 ($)
Run Code Online (Sandbox Code Playgroud)

另一个例子:

model = iPhone 5s
storage = 16
price = 150
Run Code Online (Sandbox Code Playgroud)

现在我想让我的程序通知我是否可以通过购买和转售来达成一笔好交易,我的问题是我如何以最有效的方式做到这一点?

我知道我可以使用 if 语句,但是有什么方法可以避免我编写大量不同的 if 或 elif 语句?

例子:

if model == "iPhone 7" and storage == 64 and price <= 700:
print("do_something")
Run Code Online (Sandbox Code Playgroud)

这只是 1 个模型和存储选项的大量代码。如果我要使用这种方法,我将不得不再写 29 个。

python if-statement python-3.x

5
推荐指数
1
解决办法
4815
查看次数

标签 统计

if-statement ×1

python ×1

python-3.x ×1