如何确定变量的优先级(Python)

时间:2019-03-25 21:36:36

标签: python

好的,所以我一直遇到一个问题,那就是将变量的优先级与长度无关。我希望优先考虑卓越学分,因为这是他在NCEA中获得最高价值的学分,然后我希望优先对功绩进行优先考核,直到获得最佳80学分为止。

到目前为止,我仅看到有关使用max()的示例,这不是我所需要的

#Ncea Calculator
EXCELLENCE_THRESHOLD = 50
MERIT_THRESHOLD = 50

user_input_achieved = input("How many NCEA achieved credits do you have: ")
total_achieved_credits = int(user_input_achieved)

user_input_merit = input("How many NCEA merit credits do you have: ")
total_merit_credits = int(user_input_merit)

user_input_excellence = input("How many NCEA excellence credits do you have: ")
total_excellence_credits = int(user_input_excellence)

excellence_endorsement_applies = False
if total_excellence_credits >= EXCELLENCE_THRESHOLD:
   excellence_endorsement_applies = True

merit_endorsement_applies = False
if total_merit_credits + total_excellence_credits >= MERIT_THRESHOLD:
   merit_endorsement_applies = True

print('You have a total of', total_achieved_credits + total_merit_credits + total_excellence_credits ,'NCEA credits')

if excellence_endorsement_applies == True:
   print("You also got an overall excellence endorsement, well done!")

if merit_endorsement_applies == True and excellence_endorsement_applies == False:
   print("You got an overall merit endorsement, well done but there's still room for improvement!")

total_credits_this_year = total_achieved_credits + total_merit_credits + total_excellence_credits

if total_credits_this_year >= int(80):
   print("You passed Ncea Level 1!")

if total_credits_this_year < int(79):
   print("You failed Ncea Level 1, come on man!")

rank_score_achieved = int(user_input_achieved) * int(2)
rank_score_merit = int(user_input_merit) * int(3)
rank_score_excellence = int(user_input_excellence) * int(4)

total_rank_score = rank_score_achieved + rank_score_merit + rank_score_excellence

print ('your total rank score is', total_rank_score)

所以,如果用户写了:

How many achieved credits:20
How many merit credits:30
How many excellence credits:50

那么我希望它选出最好的80个,以便它选出50个优秀,然后选30个功绩学分,然后计算排名得分。

1 个答案:

答案 0 :(得分:0)

这很难理解,但是我相信OP只是试图使用name作为排序索引来求和直到值达到80。
传统上,您将这些name:value对存储在字典中,而不是变量中,例如:

data = {'a': 60, 'b': 30, 'c': 50, 'd': 20}

然后,您可以简单地遍历sorted()字典,直到达到总数,例如:

In []:
results = []
total = 0
for k in sorted(data, reverse=True):
    total += data[k]
    if total >= 80:
        results.append((k, data[k]+80-total))
        break
    results.append((k, data[k]))
results

Out[]:
[('d', 20), ('c', 50), ('b', 10)]