I'm attending an AP CSP class and I'm trying to turn this pseudocode:
into runnable Python code.
This is what I've coded:
words = ["song", "book", "video", "book"]
index = 1
n = len(words)
for i in range (n):
if words[index] == "book":
words.insert(index, "read")
index = index 1
else:
index = index 1
print(words)
And this is the intended result:
["song", "read", "book", "video", "read", "book"]
But after running the code segment I coded, this is what I got:
['song', 'read', 'read', 'read', 'read', 'book', 'video', 'book']
Can anyone help me?
CodePudding user response:
You should delete the "else" sentence, and pull the indentation of the below line.
like this
words = ["song", "book", "video", "book"]
index = 1
n = len(words)
for i in range (n):
if words[index] == "book":
words.insert(index, "read")
index = index 1
index = index 1
print(words)