我有一些固定变量,我想与一些用户输入进行比较。基于结果我想打印一个结果,但是如何比较变量而不调用许多if语句并且必须将每个变量相互比较?
#Nutrition Class and methods
class Nutrition():
def __init__(self):
self.pro = pro
self.vit = vit
self.carb = carb
self.fat = fat
self.mineral = mineral
def comp_nut(self, pro, vit, fat, carb, mineral):
if(self.pro >= req_pro) and (self.vit>= req_vit) and (self.fat >= req_fat) and (self.carb >= req_carb) and (self.mineral >= req_min):
print("You are healthy")
elif (self.pro >= req_pro) and (self.vit>= req_vit) and (self.fat >= req_fat) and (self.carb >= req_carb) and (self.mineral < req_min):
print("Mineral is low!")
print("Require Amount is "+ str(req_min))
elif (self.pro >= req_pro) and (self.vit>= req_vit) and (self.fat >= req_fat) and (self.carb < req_carb) and (self.mineral >= req_min):
print("Carb is low!")
print("Require Amount is "+ str(req_carb))
elif (self.pro >= req_pro) and (self.vit>= req_vit) and (self.fat < req_fat) and (self.carb >= req_carb) and (self.mineral >= req_min):
print("Fat is low!")
print("Require Amount is "+ str(req_fat))
elif (self.pro >= req_pro) and (self.vit < req_vit) and (self.fat >= req_fat) and (self.carb >= req_carb) and (self.mineral >= req_min):
print("Vitamin is low!")
print("Require Amount is "+ str(req_vit))
elif (self.pro < req_pro) and (self.vit>= req_vit) and (self.fat >= req_fat) and (self.carb >= req_carb) and (self.mineral >= req_min):
print("Protein is low!")
print("Require Amount is "+ str(req_pro))
else:
print('More than 1')
# User Input
pro = int(input("Enter Protein: "))
vit = int(input("Enter Vitamin: "))
fat = int(input("Enter Fat: "))
carb = int(input("Enter Carbs: "))
mineral = int(input("Enter Mineral: "))
答案 0 :(得分:1)
一次测试一件事,如下:
def comp_nut(self, pro, vit, fat, carb, mineral):
if (self.mineral < req_min):
print("Mineral is low!")
print("Require Amount is "+ str(req_min))
elif (self.carb < req_carb):
print("Carb is low!")
print("Require Amount is "+ str(req_carb))
elif (self.fat < req_fat):
print("Fat is low!")
print("Require Amount is "+ str(req_fat))
elif (self.vit < req_vit):
print("Vitamin is low!")
print("Require Amount is "+ str(req_vit))
elif (self.pro < req_pro):
print("Protein is low!")
print("Require Amount is "+ str(req_pro))
else:
print("You are healthy.")
这将打印缺陷检查成功的第一条消息,或者#34;您是健康的。&#34;如果所有检查都失败了。
答案 1 :(得分:1)
为避免每次都重复所有字段,我会做这样的事情:
from collections import namedtuple
# Use a namedtuple so that we don't have to write __init__()
# and so that all the field names are stored in _fields.
class Nutrition(namedtuple('Nutrition', 'protein vitamin fat carb mineral')):
# List of minimum requirement per each
# field (these are random numbers)
min_requirements = {
'protein': 50,
'vitamin': 60,
'fat': 120,
'carb': 30,
'mineral': 40,
}
def check(self):
healthy = False
# Instead of checking the fields one by one,
# use a loop over _fields.
for field in self._fields:
value = getattr(self, field)
expected = self.min_requirements[field]
if value < expected:
print('{} is low!'.format(field.capitalize()))
print('Required amount is {}'.format(expected))
healthy = False
if healthy:
print('You are healthy')
使用示例:
n = Nutrition(
protein=int(input('Enter Protein: ')),
vitamin=int(input('Enter Vitamin: ')),
fat=int(input('Enter Fat: ')),
carb=int(input('Enter Carbs: ')),
mineral=int(input('Enter Mineral: ')),
)
n.check()
这样做的好处是,如果你想添加/删除一个字段,这是一个微不足道的变化。此外,如果你想添加更复杂的条件(例如检查最大值而不是最小值),这也是一个微不足道的变化。
我使用namedtuple主要是为了懒惰。这会产生副作用,即您的字段是只读的,但即使没有命名元组,您也可以轻松地获得相同的结果。