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 ))
用py算学分绩点
最新推荐文章于 2022-01-20 20:22:38 发布