在这个简单的Gpa计算器中添加迭代的有效方法。 1输入中还有2个变量?

时间:2015-11-26 04:11:07

标签: python

我实际上有2个问题。以下是一个简单的GPA计算器。

第一个问题:我为gpa制作了一个列表,为信用证制作了一个列表。然后我做了sum(list)来添加每个列表中的每个元素。这是获得每个类别(gpa和信用)总和的正确/有效方式吗?

第二个问题:在此代码中,我要求用户在下面的行中输入他们的标记和信用。如何通过逗号分隔1行中的两个值。例如,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)

1 个答案:

答案 0 :(得分:1)

您对第一个问题的sum想法是正确的。

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

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)应用于第二个参数中的每个元素,这是一个可迭代的。