I’m writting some code in which I try to check every number in a range using a for loop and an if statement. The loop stops after the first check, and throws an error with message wrong type ,,None,, expected integer, I’ll give you what I have been asked to do:
Define a function findLine(prog, target) to perform the following. Assume prog is a list of strings containing a BASIC program, like the type generated by getBASIC(); assume target is a string containing a line number, which is the target of a GOTO statement. The function should return the index i (a number between 0 and len(prog)-1) such that prog[i] is the line whose label equals target. Hint
Sample input/output: If you call
findLine([’10 GOTO 20′,’20 END’], ’10’)
def findLine(prog, target):
L=[]
for i in range(0, len(prog)):
L=L+prog[i].split()
for j in range(0,len(L)):
i=L.index(L[j])
if j == int(target):
i=i//3
return i
These is some information :
Running findLine(['10 GOTO 20', '20 END'], '20')… Error: findLine(['10 GOTO 20', '20 END'], '20') has wrong type "None" Type, expected Integer
The loop isn’t stopping after the first check: instead, it’s never successfully returning. This function returns based on the following loop:
But if the condition
j == int(target)is never satisfied, the loop will finish, and the functionfindLinewill returnNone.ETA: Now that you’ve explained what the task is: your construction of the
Llist isn’t the right way to go about the problem:This will produce
['10', 'GOTO', '20', '20', 'END']. In this case, you don’t know which line is which! Still, splitting it is the right direction. What if you just add the label from each split line:That will produce an
Lequal to['10', '20'], which is much closer to what you’re looking for. The last step would be to get the index of the target in that array (which you already know how to do!)