在这个简单的Gpa计算器中添加iterables的有效方法。在一个输入中还有两个变量?

2024-05-16 18:55:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个问题。下面是一个简单的GPA计算器。你知道吗

第一问:我做了一份gpa名单和一份学分名单。然后我做了sum(list)把每个列表中的元素加起来。这是一个适当/有效的方式获得每个类别的总和(平均绩点和学分)?你知道吗

第二个问题:在这个代码中,我要求用户在下面的行中输入他们的分数和信用。如何在一行中得到两个值,并用一个逗号。所以呢例如,95,0.5。(我需要一切都是浮子)

我试过这样做,但显然是错的。你知道吗

  mark,credit=float(input('mark'+ str(Markcount)+':','credit'+ str(Creditcount)+':').split(','))

完整代码如下:

    GpaList=[]
    CreditList=[]
    Markcount=0
    Creditcount=0

print("Welcome to the GPA calculator.")   

courses=int(input("enter amount of courses"))

print('Enter marks in percentage and then credit for that course in the next line')

for a in range(courses):
    Markcount+=1
    Creditcount+=1
    mark=int(input('mark'+ str(Markcount)+':'))
    credit=float(input('credit'+ str(Creditcount)+':'))

    if mark>=85 and mark<=100:
        gpa=float(4.0)


    elif mark>=80 and mark<=84:
        gpa=float(3.7)

    elif mark>=77 and mark<=79:
        gpa=float(3.3)

    elif mark>=73 and mark<=76:
        gpa=float(3.0)

    elif mark>=70 and mark<=72:
        gpa=float(2.7)

    elif mark>=67 and mark<=69:
        gpa=float(2.3)

    elif mark>=63 and mark<=66:
        gpa=float(2.0)

    elif mark>=60 and mark<=62:
        gpa=float(1.7)

    elif mark>=57 and mark<=59:
        gpa=float(1.3)

    elif mark>=53 and mark<=56:
        gpa=float(1.0)

    elif mark>=50 and mark<=52:
        gpa=float(0.7)

    else:
        gpa=float(0.0)




    weightGpa= float(gpa*credit)
    (GpaList.append(weightGpa))
    (CreditList.append(credit))

    totalSum=float(sum(GpaList))
    totalCredit=float(sum(CreditList))
    FinalGpa=totalSum/totalCredit
    print(FinalGpa)

Tags: andinputfloatmarksumprintcreditgpa
1条回答
网友
1楼 · 发布于 2024-05-16 18:55:09

你对第一个问题的想法是正确的。你知道吗

对于第二个问题,你可以这样做:

mark = float(input('mark'+ str(Markcount)+':'))
credit = float(input('credit'+ str(Creditcount)+':'))

如果你想把它从输入中提取出来,你可以这样做:

mark,credit = map(float, input('mark'+ str(Markcount)+' and credit'+ str(Creditcount)+':').split(','))

map将第一个参数、函数float应用于第二个参数中的每个元素,这是一个iterable。你知道吗

相关问题 更多 >