I'm brand new to programming and have been working on a free Python course from the University of Waterloo. I'm currently stuck on Section 15A, where I have to write a BASIC simulator. I'm working on the section titled "Smart Simulation", where I'm writing the code to execute the BASIC program.
Part of the exercise is having to determine if the program completed successfully, or if it entered an infinite loop. Here is the current code I have for this section:
def findLine(prog, target):
for x in range(0,len(prog)):
prog2 = prog[x].split()
start = prog2
if len(prog2) > 2:
end = prog2
if start == target:
location = 0
visited = [False] * len(prog)
if location==len(prog)-1: return "success"
if visited[location] == True: return "infinite loop"
currentLine = prog[location]
currentLine = currentLine.split()
T = currentLine
location = findLine(prog, T)
visited[location] = True
execute(['10 GOTO 21', '21 GOTO 37', '37 GOTO 21', '40 END'])
execute(['5 GOTO 30', '10 GOTO 20', '20 GOTO 10', '30 GOTO 40', '40 END'])
You almost did it!
you just misplaced the assignment order, it should be like:
T = currentLine visited[location] = True location = findLine(prog, T)