python求平均成绩 输入用空格分隔的两个代码_在Python2.7中,如何计算以空格分隔的多个输入?...

您需要分割包含原始标记的字符串,然后使用checkGrade函数来获取该原始标记的分数。试试这个:def checkGrade(raw):

if raw>100:

score = "Error, raw score exceed max score"

elif raw>80:

score = 4.00

elif raw>70:

score = 3.00

elif raw>60:

score = 2.00

elif raw>50:

score = 1.00

elif raw>0:

score = 0

else:

score = "Error, raw score exceed min score"

return score

def calGPA(s):

raw_marks = s.split()

num_subjects = len(raw_marks)

if num_subjects > 10:

print('Limit for number of subjects exceeded')

return

total = 0

for raw in raw_marks:

total += checkGrade(int(raw))

print('GPA: {}'.format(total/num_subjects))

calGPA("77 45 77 55 87")

输出:

^{pr2}$

编辑以反映您的新要求def calGPA(s):

gpas = s.split()

valid_grades = ['A', 'B', 'C', 'D', 'F']

if not all(gpa in valid_grades for gpa in gpas):

print("Error")

p.S.:编辑问题使当前接受的答案无效是不允许并且非常不鼓励。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值