【问题标题】:Python - TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'Python - TypeError:/:'int'和'NoneType'不支持的操作数类型
【发布时间】:2015-11-16 03:58:44
【问题描述】:

问题

我正在制作一个可以告诉你成绩的程序。我已经得到了所有的用户输入,现在我需要做一些计算。调用 calculate_weighted_average 时出现我的问题。它将参数作为整数(至少我认为它确实如此:我要求它 print(x, y, z) 并打印两次,一次作为 x、y、z 的正确输入,一次作为 x 的正确输入,但它打印 y 和z as None) 但它拒绝执行我要求它执行的数学运算。函数不应该能够接受参数并执行这个简单的操作:( x + y ) * z 吗?
所以由于某种原因 y 和 z 更改为 None 这是我需要修复的!

守则

def main():
    tests = get_initial_input("tests")
    assignments = get_initial_input("assignments")
    exercises = get_initial_input("exercises")
    labs = get_initial_input("labs")
    finals = get_initial_input("finals")
    testsp = get_percents(tests , "tests")
    assignmentsp = get_percents(assignments, "assignments")
    exercisesp = get_percents(exercises, "exercises")
    labsp = get_percents(labs, "labs")
    finalsp = get_percents(finals, "finals")
    testst = get_totals(tests, "tests")
    assignmentst = get_totals(assignments, "assignments")
    exercisest = get_totals(exercises, "exercises")
    labst = get_totals(labs, "labs")
    finalst = get_totals(finals, "finals")
    testss = get_scores(tests, "tests")
    assignmentss = get_scores(assignments, "assignments")
    exercisess = get_scores(exercises, "exercises")
    labss = get_scores(labs, "labs")
    finalss = get_scores(finals, "finals")
    testsz = calculate_weighted_average(testss, testst, testsp)
    assignmentsz = calculate_weighted_average(assignmentss, assignmentst, assignmentsp)
    exercisesz = calculate_weighted_average(exercisess, exercisest, exercisesp)
    labsz = calculate_weighted_average(labss, labst, labsp)
    finalsz = calculate_weighted_average(finalss, finalst, finalsp)
def get_initial_input(x):
    val = int(input("How many "+ x + " were there?    "))
    return val

def get_percents(x, string):
    if x > 0:
            xp = int(input("What percentage are "+ string + " weighted?       "))
            return xp

def get_totals(x, string):
    if x > 0:
            xt = int(input("How many total points available in the "+ string +" category?      "))
            return xt

def get_scores(x, string):
    total = 0
    for y in range(x):
            xs = int(input("Scores for "+ string +" ?:     "))
            total = total + xs
    return total
def calculate_weighted_average(x, y, z):
    print(x, y, z)
    this = ( x / y ) * z
    return this

main()

追溯

How many tests were there?    2
How many assignments were there?    0
How many exercises were there?    0
How many labs were there?    0
How many finals were there?    0
What percentage are tests weighted?       20
How many total points available in the tests category?      20
Scores for tests ?:     20
Scores for tests ?:     20
40 20 20
0 None None
Traceback (most recent call last):
  File "assignment7.py", line 61, in <module>
    main()
  File "assignment7.py", line 23, in main
    assignmentsz = calculate_weighted_average(assignmentss, assignmentst, assignmentsp)
  File "assignment7.py", line 49, in calculate_weighted_average
    this = ( x / y ) * z
TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'

提前致谢

【问题讨论】:

标签: python


【解决方案1】:

您需要调整get_totalsget_percents 以返回int,即使它们的条件失败。照原样,如果他们的任何一个条件不满足 - None 将被返回。

例如:

def get_percents(x, string):
    xp = 0
    if x > 0:
        xp = int(input("What percentage are "+ string + " weighted?       "))
    return xp

还有:

def get_totals(x, string):
    xt = 0
    if x > 0:
        xt = int(input("How many total points available in the "+ string +" category?      "))
    return xt

【讨论】:

    【解决方案2】:

    功能

    def get_totals(x, string):
      if x > 0:
            xt = int(input("How many total points available in the "+ string +" category?      "))
            return xt
    

    x 小于或等于零时返回None。该值稍后会传播到 calculate_weighted_average 中的数值表达式并破坏它。 修复此函数和其他函数以始终返回数字或引发异常。

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 2017-12-27
      • 2014-03-31
      • 2021-05-23
      • 2015-10-15
      • 2016-08-25
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多