我正在制作一个模块来实现计算机的基础知识并模拟这些东西,但是在处理内存的代码的特定部分中 - 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 = 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)
以下是Test.py的代码:
import computer
comp = computer.Computer()
mem = computer.Memory()
cpu = computer.CPU()
comp.Boot('', 'shw (\'How are you doing?\')', mem, mem.Memory, cpu)
这是错误:
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