【问题标题】:need help finding the weighted average需要帮助找到加权平均值
【发布时间】:2013-11-21 00:55:42
【问题描述】:

在我的代码中出现错误 回溯(最近一次通话最后): 文件“C:/Users/Daniel Nase/Desktop/assignment 7 test.py”,第 119 行,在 主要的() 文件“C:/Users/Daniel Nase/Desktop/assignment 7 test.py”,第 23 行,在 main 计算加权平均(score_ammount,scores_t,score_weight,average1,num_t) 文件“C:/Users/Daniel Nase/Desktop/assignment 7 test.py”,第 112 行,在 calculate_weighted_average weighted_average_t = float(sum(average1))*float((score_weight.pop(0))/100) TypeError: 不支持的操作数类型 /: 'str' 和 'int'

我正在尝试计算加权平均值,但我不知道我做错了什么... 请帮忙!!!!

def main():
     score_type = ["tests","assignments","quizzes","labs"]
     score_ammount = [0,0,0,0]
     score_weight = [0,0,0,0]
     final_weight = 0

     scores_t = [0]
     scores_a = [0]
     scores_q = [0]

     total = 0
     average1 = [0]
     average2 = [0]
     average3 = [0]

     num_t = 0




     get_initial_input(score_type,score_ammount,score_weight)
     get_scores(score_ammount,average1,average2,average3,total,scores_t,scores_a,scores_q)
     calculate_weighted_average(score_ammount,scores_t, score_weight, average1, num_t)



def get_initial_input(score_type,score_ammount,score_weight):
    for x in range(0,4):
        score_ammount[x] = input("How many " + score_type[x] + " are there?")

    if input("Is there a final with a serperate weight?(1 = yes/0 = no) ") == '1':
        final_weight = int(input("What is the weight of the final?(0-100) "))
    else:
        final_weight = ""

    total_weight = 0
    while total_weight != 100:
        total_weight = 0
        print("")
        print("Please make sure the weight of all scores add up to 100")
        for x in range(0,3):
            score_weight[x] = input("What is the weight of all the " + score_type[x])
            total_weight = total_weight + int(score_weight[x])
    return score_ammount, score_weight, final_weight


def get_scores(score_ammount,average1,average2,average3,total,scores_t,scores_a,scores_q):

    num_t = int(score_ammount.pop(0))
    print("")

    for i in range(0,num_t):
        scores_t.append(0)

    for i in range(0, num_t):
        y = i + 1
        scores_t[i] = int(input("Please input test score #" + str(y) + " "))

    print(" ")

    for i in range(0,1):
        total = total + sum(scores_t)
    average1 = total/num_t
    print(int(average1))


###########################################################################

    num_a = int(score_ammount.pop(0))
    print("")

    for i in range(0,num_a):
        scores_a.append(0)

    for i in range(0, num_a):
        y = i + 1
        scores_a[i] = int(input("Please input assignment score #" + str(y) + " "))

    print(" ")
    total = 0
    for i in range(0,1):
        total = total + sum(scores_a)
    average2 = total/num_a
    print(int(average2))

###########################################################################

    num_q = int(score_ammount.pop(0))
    print(" ")

    for i in range(0,num_q):
        scores_q.append(0)

    for i in range(0, num_q):
        y = i + 1
        scores_q[i] = int(input("Please input quiz score #" + str(y) + " "))

    print(" ")
    total = 0
    for i in range(0,1):
        total = total + sum(scores_q)
    average3 = 0 + total/num_q
    print(int(average3))

    return average1, average2, average3, total, scores_t, scores_a, scores_q, num_t, num_a, num_q

def calculate_weighted_average(score_ammount, scores_t, score_weight, average1, num_t):
    num_t = int(score_ammount.pop(0))
    for i in range (0,num_t):
        average1[i] = sum(scores_t)/num_t

        weighted_average_t = float(sum(average1))*float((score_weight.pop(0))/100)
        print(weighted_average_t)





main()

【问题讨论】:

  • 您认为错误消息TypeError: unsupported operand type(s) for /: 'str' and 'int' 可能意味着什么?您认为它在告诉您您正在尝试做什么?
  • 我正在尝试将 str 和 int 相乘,但我不知道该怎么做才能获得 ammount1 和 int,我可以使用它们来获得加权平均值跨度>

标签: python-3.2 weighted-average


【解决方案1】:

这不是你试图将一个字符串和一个整数相乘,而是你试图将它们相除(错误消息显示/,而不是*)。不过,您可以轻松地将字符串转换为 intfloat,就像您在这些行中所做的那样:

final_weight = int(input("What is the weight of the final?(0-100) "))

scores_t[i] = int(input("Please input test score #" + str(y) + " "))

scores_a[i] = int(input("Please input assignment score #" + str(y) + " "))

scores_q[i] = int(input("Please input quiz score #" + str(y) + " "))

input 总是返回一个字符串,如果你想从中得到一个数字,你必须转换它。你在这里没有这样做:

score_ammount[x] = input("How many " + score_type[x] + " are there?")

这里

        score_weight[x] = input("What is the weight of all the " + score_type[x])
        total_weight = total_weight + int(score_weight[x])

您在将score_weight[x] 的值添加到total_weight 之前将其转换为整数,但score_weight[x] 本身仍然是一个字符串。

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多