【问题标题】:I am trying to make a loop pulling from multiple arrays我正在尝试从多个数组中拉出一个循环
【发布时间】:2022-01-15 10:17:13
【问题描述】:

我正在制作一个简单的计算器,除其他外,它可以为用户提供他们之前计算的格式化历史记录。

 print('Formmated as an equation')
 for x in range(len(finalhistory)):
    print(num1history(x) + float(operationHistory(x)) + num2history(x) + ' = ' + 
    finalhistory(x))
    print(' ')
 return

不过,当它运行时,我会收到一条错误消息:

Exception has occurred: TypeError
'list' object is not callable
 File "MCT.py", line 34, in BH
    print(num1history(x) + operationHistory(x) + num2history(x) + ' = ' + finalhistory(x))

编辑: 应该澄清调用的历史是数组。 num1historynum2historyfinalhistory 存储 float 值,operationHistory 存储 str 值。

【问题讨论】:

    标签: python arrays for-loop debugging


    【解决方案1】:

    finalhistory 是一个列表。我们不能像函数一样调用它。

    finalhistory(x) -> 是错误。

    num1historyoperationHistorynum2history 相同

    您可能打算使用finalhistory[x]num1history[x]operationHistory[x]num2history[x]

    【讨论】:

      【解决方案2】:

      你用方括号索引列表

       print('Formmated as an equation')
       for x in range(len(finalhistory)):
          print(f'{num1history[x]} + {float(operationHistory[x])} + {num2history[x]} +  =  + 
          {finalhistory[x]}')
          print(' ')
       return
      

      您还应该使用 f-string 打印

      【讨论】:

        【解决方案3】:

        从代码中可以看出,您正在使用列表,并且列表不可调用。 解决方案: 对其他数组使用finalhistory[x]...类似

        【讨论】:

          猜你喜欢
          • 2016-11-11
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-26
          • 1970-01-01
          相关资源
          最近更新 更多