我在Python3.5中有另一个类型错误
我正在制作一个模块来实现计算机的基础知识,并模拟这些东西,但在处理内存的代码的特定部分,Python给了我一个似乎无法修复的错误 以下是computer.py的代码:我在Python3.5中有另一个类型错误,python,class,python-3.x,cpu,error-suppression,Python,Class,Python 3.x,Cpu,Error Suppression,我正在制作一个模块来实现计算机的基础知识,并模拟这些东西,但在处理内存的代码的特定部分,Python给了我一个似乎无法修复的错误 以下是computer.py的代码: def nop(): nop = 'nop' class Computer: def __init__(self): self.Started = True def Boot(self, RAM, OS, MemoryObj, Memory, CPU): self.RAM
def nop():
nop = 'nop'
class Computer:
def __init__(self):
self.Started = True
def Boot(self, RAM, OS, MemoryObj, Memory, CPU):
self.RAM = RAM
self.Memory = Memory
self.CPU = CPU
MemoryObj.addMem(OS, Memory, 0)
if len(Memory) < 1000:
diffrence = 1000 - len(Memory)
for x in range(diffrence):
MemoryObj.addMem('d')
else:
nop()
MemoryObj.addMem(RAM, Memory, 1000)
CPU.start()
class Memory:
def __init__(self):
self.Memory = []
self.Address = []
for x in range(5096):
self.Memory.append('0')
self.Address.append(x)
def addMem(self, data, Memory, lastAddr):
LenData = len(data)
for x in range(LenData):
Memory.insert(data[x], x + lastAddr)
def GetMemByte(self, Memory, addr):
return(Memory[addr])
def GetMemLen(self, Memory, length, startaddr):
for x in range(length):
byte = self.GetMemByte(Memory, startaddr + x)
if byte == ' ':
GetMem = GetMem + '-'
elif byte == ';':
GetMem = GetMem + 's'
else:
GetMem = GetMem + byte
return(GetMem)
class CPU:
def __init__(self):
self.codes = {
'shw': 'print'
}
def start(self, MemoryObj, Memory):
OSCODE = MemoryObj.GetMemLen(Memory, 1000, 0)
self.execute(OSCODE)
def execute(self, Code):
codebase = self.codes
_temp = ''
while True:
for x in range(len(Code)):
if Code[x] == 's':
lengoto = x
break
else:
_temp = _temp + Code[x]
if lengoto == len(Code):
break
else:
_temp.split('-')
func = _temp[0]
ourfunc = codebase[func]
pystring = ourfunc
varable = _temp[1]
pystring = pystring + varable
exec(pystring)
以下是错误:
Traceback (most recent call last):
File "/private/var/root/Documents/Test1.py", line 9, in <module>
comp.Boot('', 'shw (\'How are you doing?\')', mem, mem.Memory, cpu)
File "/private/var/root/Documents/computer.py", line 13, in Boot
MemoryObj.addMem(OS, Memory, 0)
File "/private/var/root/Documents/computer.py", line 41, in addMem
Memory.insert(data[x], x + lastAddr)
TypeError: 'str' object cannot be interpreted as an integer
回溯(最近一次呼叫最后一次):
文件“/private/var/root/Documents/Test1.py”,第9行,在
comp.Boot(“'shw(\'How'down?\”)”,mem,mem.Memory,cpu)
文件“/private/var/root/Documents/computer.py”,第13行,在Boot中
MemoryObj.addMem(操作系统,内存,0)
addMem中第41行的文件“/private/var/root/Documents/computer.py”
Memory.insert(数据[x],x+lastAddr)
TypeError:“str”对象不能解释为整数
数据[x]
是一个字符串<代码>内存是一个列表<代码>列表。insert需要一个整数作为其第一个参数。您将类型内存
与计算机
上的属性内存
以及内存
类型上的属性内存
混为一谈,我非常困惑,使用Memory
变量,您可以不断传递到Memory
类型的方法中(而不是使用self
引用当前实例)。你知道你在那里做什么吗?然后,请重命名所有这些变量,使其更有意义(例如,非类型不应使用标题大小写)。
Traceback (most recent call last):
File "/private/var/root/Documents/Test1.py", line 9, in <module>
comp.Boot('', 'shw (\'How are you doing?\')', mem, mem.Memory, cpu)
File "/private/var/root/Documents/computer.py", line 13, in Boot
MemoryObj.addMem(OS, Memory, 0)
File "/private/var/root/Documents/computer.py", line 41, in addMem
Memory.insert(data[x], x + lastAddr)
TypeError: 'str' object cannot be interpreted as an integer