首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python 2.7返回值和添加函数

Python 2.7返回值和添加函数
EN

Stack Overflow用户
提问于 2018-06-20 04:00:41
回答 1查看 50关注 0票数 0

以下是打印和计算加权考试成绩的代码:

代码语言:javascript
复制
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)? "))

代码语言:javascript
复制
def exam_2():
    print("\nMidterm 2:")
    exam(input("Weight 0-100? "),input("Score earned? "),input("Were scores shifted (1= yes 2=no)? "))

代码语言:javascript
复制
overall_percentage =int(exam_1() +exam_2())

我希望代码使用返回的weighted_score将加权分数相加在一起。尽管如此,当我运行代码时,它给出了这个错误:

代码语言:javascript
复制
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

我该如何解决这个问题?

这也是一个示例输出--除了overall_percentage之外,所有的东西都可以工作,因为它让我认为我的返回语句出了问题:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-06-20 04:04:08

您的函数exam_1exam_2不返回任何内容(= None )。

这就是为什么exam_1() + exam_2()会导致None + None并抛出您所看到的异常:

TypeError:+:'NoneType‘和'NoneType’不支持的操作数类型

你可以很容易地解决这个问题:

代码语言:javascript
复制
def exam_1():
    print("Midterm 1:")
    return exam( ...

代码语言:javascript
复制
def exam_2():
    print("\nMidterm 2:")
    return exam( ...
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50936077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档