python如果elif else只返回第一个输出而不使用逻辑

时间:2017-05-09 13:31:51

标签: python django

我有一个if elif else块,它只为if语句生成输出,即使它应该返回其他条件。即使我改变条件的顺序,它总是返回第一个。我的代码如下所示:

age, education, stability ....是我定义的函数,我将它们添加到feat_list

class operable:
def __init__(self, f):
    self.f = f
def __call__(self, x):
    return self.f(x)

def op_to_function_op(op):
    def function_op(self, operand):
        def f(x):
            return op(self(x), operand(x))
        return operable(f)
    return function_op

for name, op in [(name, getattr(operator, name)) for name in dir(operator) 
    if "__" in name]:
    try:
        op(1,2)
    except TypeError:
        pass
    else:
        setattr(operable, name, op_to_function_op(op))

@operable
def age(ModelAdmin, request, queryset):
    age = 0
    if customer.Age == '60 +':
        age = 0
    elif customer.Age == '36 - 59':
        age = 1
    else:
        age = 2
    return age

def education(ModelAdmin, request, queryset):
    education = 0
    if customer.Education == 'Highschool and below':
        education = 0
    else:
        education = 1
    return education

def employment(ModelAdmin, request, queryset):
    employment = 0
    if customer.Employment == 'Student':
        employment = 0
    elif customer.Employment == 'Contract':
        employment = 1
    else:
        employment = 2
    return employment

def stability(ModelAdmin, request, queryset):
    stability = 0
    if customer.Employer_Stability == 'Unstable':
        stability = 0
    else:
        stability = 1
    return stability

def residential(ModelAdmin, request, queryset):
    residential = 0
    if customer.Residential_Status == 'Rented':
        residential = 0
    else:
        residential = 1
    return residential

def salary(ModelAdmin, request, queryset):
    salary = 0
    if customer.Salary <= 1000:
        salary = 0
    elif 1000 < customer.Salary <= 10001:
        salary = 1
    else:
        salary = 2
    return salary

def loyalty(ModelAdmin, request, queryset):
    loyalty = 0
    if customer.Customer_Loyalty <= 2:
        loyalty = 0
    else:
        loyalty = 1
   return loyalty

def balance(ModelAdmin, request, queryset):
    balance = 0
    if customer.Balance <= 2500:
        balance = 0
    elif 2500 < customer.Balance <= 10001:
        balance = 1
    else:
        balance = 2
    return balance

def feat_list():
    total = age + education + employment + stability + residential + salary 
        + loyalty + balance
return total

for customer in queryset:

    if feat_list() < 12:
        customer.Service_Level = Service.objects.get(service_name = 'Silver 
            Package')
        silver_customers.append(customer.Name)

    elif 11 < feat_list() <= 15:
        customer.Service_Level = Service.objects.get(service_name = 'Gold 
            Package')
        gold_customers.append(customer.Name)

    else:
         customer.Service_Level = Service.object.get(service_name = 
            "Platinum Package")
         platinum_customers.append(customer.name)
   customer.save()

如何更改这些语句以使其不使用逻辑?

3 个答案:

答案 0 :(得分:1)

如果你说feat_list() < 12 dand值是11.5,那么它是真的,并执行firt if,但下一个条件也是真的11 < feat_list() <= 15, 因为11.5,是11到15之间。  我的观点是......你在第一个条件中的第一个条件是,#34;包含&#34;在elif的第二个条件中:

12介于11和15之间,因为11 < 12 <= 15,所以,第一个条件是错误的。

答案 1 :(得分:0)

仅当if不为真时才会评估elif

因此,如果您想检查11 < feat_list() <= 15:即使feat_list() < 12:为真,请使用if代替elif

答案 2 :(得分:0)

在您的示例中,Silver和Gold包之间的比较范围重叠(11-12)。此外,您可能不希望白金套餐成为&#34;默认&#34;一个(没有检查,只要它是最高级别)。如果用以下样式(伪代码)重写比较,可能会更清楚代码在做什么:

flist = feat_list()
# You can check the returned value here, ie. print(flist), and the value
# won't change before you're finished with the comparison.

if flist > 15:
    #...Platinum processing...
elif flist > 11:
    #...Gold processing...
else:
    #...Silver processing...