Python ';列表';对象不可调用,正在尝试访问列表中的项目并将其分配给对象
我运行的代码是用来平衡普通化学的化学方程式。我从中运行的文件包含以下代码:Python ';列表';对象不可调用,正在尝试访问列表中的项目并将其分配给对象,python,Python,我运行的代码是用来平衡普通化学的化学方程式。我从中运行的文件包含以下代码: from equation import Equation from time import sleep def run_balance(): # Runs the chemical equation balance algorithm print('=================================================') print('Insert chemic
from equation import Equation
from time import sleep
def run_balance():
# Runs the chemical equation balance algorithm
print('=================================================')
print('Insert chemical equation with elements in parentheses followed by the number of atoms:')
print('Example: (H)2 + (O)2 = (H)2(O)1')
user_input = str(input('>>> '))
try:
equation = Equation(user_input)
print('Balanced equation: ' + equation.balance())
sleep(3)
run_balance()
except IndexError:
print('Invalid input...')
sleep(3)
run_balance()
run_balance()
等式
类所在的文件包含以下代码:
class Equation:
# Create a class for chemical equations
def __init__(self, equation):
# Initialize equation into an object the code understands
self.left = list()
self.right = list()
self.balanced = True
integers = '0123456789'
split = equation.split(' = ')
left = split[0]
right = split[1]
left_components = left.split(' + ')
right_components = right.split(' + ')
total_left = dict()
total_right = dict()
当我提供任何输入时,我收到以下错误:
14 integers = '0123456789'
15 split = equation.split(' = ')
---> 16 left = split[0]
17 right = split[1]
18 left_components = left.split(' + ')
TypeError: 'list' object is not callable
我很困惑,因为我理解这个错误通常来自于在试图访问列表中的项目时使用括号,您只需将括号更改为方括号。但是,我试图在拆分后将
left
指定为方程的前半部分。有什么建议吗?你的回溯毫无意义,可能是有原因的。试着找出那个原因
有时,这种情况下的问题是,您有一个旧的.pyc
文件,它来自以前版本的代码。例如,如果您有一个等式
库,以及一个等式.py
在某个点被删除,则可能发生这种情况。您提供了什么输入?我尝试了'1+1=2'
但没有得到错误。您也可以直接从拆分中解包:字符串的拆分方法返回一个列表,您知道,因为您选择了它的第0个和第1个元素,但您可以这样写:左,右=等式。拆分('='))
AttributeError:“Equation”对象没有“balance”属性欢迎使用StackOverflow。看见在您发布MRE代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。我怀疑您正在运行的代码与您正在查看的Python源文件不同步。