将变量赋值给Trin中数组/矩阵的单个元素的值

2024-05-11 03:29:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉堆栈溢出和Python(但对编程并不陌生)

问题-为什么韦小宝显示运行时错误-

TypeError: unsupported operand type(s) for Add: 'int' and 'list' on line 20 in main.py

在我看来,它认为l_score是一个列表,即使我在子例程中明确定义了is为int。 如何将变量分配给数组的元素,并保持变量类型与分配的类型相同,例如int? 我已经标记了发生此错误的代码部分(以及我尝试过的备选方案)

# A program to total 3 input numbers

m_score = []
total = 0


def input_scores(): # def = define.
    for i in range(0,3):
      i_score = int(input("score"))
      m_score.append([i_score])
    #end for    
    return m_score # returns the value to the calling line
# end input_scores

def CalculateMean(m_score): # def =  define. 
    tot = 0
    l_score = int(0)
    for i in m_score:
      l_score = (m_score[1])
      tot = tot + l_score            # - QUESTION 
#      tot = tot + int(l_score)       This doesn't work either
#       tot = sum(m_score)          This doesn't work either 

      print('tot = ' + str(tot)) 
      return(tot)
    #end for
    return(total)
#end   CalculateQuartiles

input_scores()
for i in m_score:
  print(i)
#end for

total = CalculateMean(m_score)
print('Total = ' + str(total))

Tags: in类型forinputreturndef错误line
1条回答
网友
1楼 · 发布于 2024-05-11 03:29:13

m_score当前是一个列表列表,因此m_score[1]仍然是一个列表,尽管只有一个项目。您在第1行中将其定义为一个空列表,然后向其追加一个单项列表

m_score.append([i_score])更改为m_score.append(i_score)

相关问题 更多 >