以下是打印和计算加权考试成绩的代码:
def exam(weight,score_earned,score_shift):
if (score_shift==1):
shift_amount=input("What was the shift amount?")
score_earned=int(shift_amount+score_earned)
if (score_earned >100):
score_earned=100
print("Total points = "+str(score_earned)+str("/100"))
weighted_score=int((score_earned/100.0)*weight)
print("Your weighted score = "+str(weighted_score)+"/"+str(weight))
return weighted_score
def exam_1():
print("Midterm 1:")
exam(input("Weight 0-100?"),input("Score earned?"),input("Were scores shifted (1= yes 2=no)? "))
def exam_2():
print("\nMidterm 2:")
exam(input("Weight 0-100? "),input("Score earned? "),input("Were scores shifted (1= yes 2=no)? "))
overall_percentage =int(exam_1() +exam_2())
我希望代码使用返回的weighted_score将加权分数相加在一起。尽管如此,当我运行代码时,它给出了这个错误:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
我该如何解决这个问题?
这也是一个示例输出--除了overall_percentage之外,所有的东西都可以工作,因为它让我认为我的返回语句出了问题:
Midterm 1:
Weight 0-100? 50
Score earned? 88
Were scores shifted (1= yes 2=no)? 2
Total points = 88/100
Your weighted score = 44/50
Midterm 2:
Weight 0-100? 50
Score earned? 88
Were scores shifted (1= yes 2=no)? 2
Total points = 88/100
Your weighted score = 44/50
发布于 2018-06-20 04:04:08
您的函数exam_1
和exam_2
不返回任何内容(= None
)。
这就是为什么exam_1() + exam_2()
会导致None + None
并抛出您所看到的异常:
TypeError:+:'NoneType‘和'NoneType’不支持的操作数类型
你可以很容易地解决这个问题:
def exam_1():
print("Midterm 1:")
return exam( ...
和
def exam_2():
print("\nMidterm 2:")
return exam( ...
https://stackoverflow.com/questions/50936077
复制相似问题