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

2024-03-28 16:32:08 发布

您现在位置: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)

因此,如果用户写道:

^{pr2}$

然后我希望它选择最好的80,这样它将选择50个优秀,然后30个优点学分,然后计算排名得分。在


Tags: inputifinttotalscorecreditsprintrank
1条回答
网友
1楼 · 发布于 2024-03-28 16:32:08

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

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

然后,您可以简单地在sorted()字典上迭代,直到得到总数,例如:

^{pr2}$

相关问题 更多 >