用py算学分绩点

def isnub( s ): # Determine whether it is a number
    try:
        float( s ) #if it is a bumber return True
        return True
    except:
        return False #if it's not a number return False
def valid_input1( s1 ):
    try:
        _a, _b = s1.split(',') # add _ in front of a, b to avoid ununsed variable warning
        return True
    except:
        return False
def valid_input2( s2 ):
    try:
        _a = s2
        return True
    except:
        return False
print("please enter the score and credit of each subject:\nenter a 'space' to star calculate\nscore,credit")
subj = [] # subject score
cred = [] # credit 
gp = [] # grade point
i = 0
sum1, sum2= 0, 0
CGPA = 0
while True:
    c = input()
    if valid_input1( c ):
        a, b = c.split(',')
    elif valid_input2( c ):
        a = c
    if a == ' ':
        break
    elif isnub( a ) and isnub( b ):
        if int( a ) >= 60:
            subj.append( int( a ) )
            cred.append( float( b ) )
        if int( a ) < 60:
            subj.append( 0 ) # score less than 60 , the gp is zero
            cred.append( float( b ) )
    else:
        print("invalid input! please enter the correct command")
if len( subj ) == False: # attention:the subj may be empty!
    print("you doesn't enter any score")
    exit()
else:
    for i0 in subj:
        if i0 != 0:
            gp.append( ( i0 - 50 ) / 10 )
        else:
            gp.append( 0 )
    for i1 in range( len( gp ) ):
        sum1 = sum1 + ( gp[i1] * cred[i1] )
    for i2 in cred:
        sum2 = i2 + sum2
        CGPA = sum1 / sum2
    print("the sum of your grade point and credit is {}, {}repectively".format( sum1, sum2 ))
    print("your CGPA is {}".format( CGPA ))
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值