I am getting lost. The instructions are not clean. Can you pls help?
Your code so far
def pin_extractor(poem, poem2, poem3):
secret_codes = []
for poem in poem, poem2, poem3:
secret_code = ''
lines = poem.split('\n')
for line_index, line in enumerate(lines):
words = line.split()
if len(words) > line_index:
secret_code += str(len(words[line_index]))
else:
secret_code += '0'
secret_codes.append(secret_code)
# User Editable Region
return secret_codes
poem = """Stars and the moon
shine in the sky
white and
until the end of the night"""
poem2 = 'The grass is green\nhere and there\nhoping for rain\nbefore it turns yellow'
poem3 = 'There\nonce\nwas\na\ndragon'
print(pin_extractor([poem, poem2, poem3]))
# User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15
I am getting lost, the instructions are really enigmatic. My last code is
def pin_extractor(poems):
secret_codes = []
poems = [poem, poem2, poem3]
for poem in poems:
secret_code = ''
lines = poem.split('\n')
for line_index, line in enumerate(lines):
words = line.split()
if len(words) > line_index:
secret_code += str(len(words[line_index]))
else:
secret_code += '0'
secret_codes.append(secret_code)
return secret_codes
print(pin_extractor([poem, poem2, poem3]))
poem = """Stars and the moon
shine in the sky
white and
until the end of the night"""
What do I do wrong? Please do not reply that I have to follow instructions, because I think the issue is that they are so unclear I am not sure what to do.
OK, so i put the print function at the very end underneath the definitions of poem, poem2, poem3, then it tells me: “cannot access local variable ‘poem’ where it is not associated with a value”
def pin_extractor(poems):
secret_codes = []
poems = [poem, poem2, poem3]
for poem in poems:
secret_code = ''
lines = poem.split('\n')
for line_index, line in enumerate(lines):
words = line.split()
if len(words) > line_index:
secret_code += str(len(words[line_index]))
else:
secret_code += '0'
secret_codes.append(secret_code)
return secret_codes
poem = """Stars and the moon
shine in the sky
white and
until the end of the night"""
poem2 = 'The grass is green\nhere and there\nhoping for rain\nbefore it turns yellow'
poem3 = 'There\nonce\nwas\na\ndragon'
print(pin_extractor([poem, poem2, poem3]))
why have you added this line?
note also that the return you have added is not in the right place, right now it’s inside the loop, meaning the loop will not loop all the times we want it to loop
reset the step, and do only the two things you are asked to do
add the return on the last line of the function
update the function call on the last line of the code
OK, so I reset, then return secret_code, uncomment, and update the argument for (poem, poem2, poem3), my code is below, but then I get the comment: You should have the function call print(pin_extractor([poem, poem2, poem3]))
def pin_extractor(poem, poem2, poem3):
secret_codes = []
for poem in poems:
secret_code = ''
lines = poem.split('\n')
for line_index, line in enumerate(lines):
words = line.split()
if len(words) > line_index:
secret_code += str(len(words[line_index]))
else:
secret_code += '0'
secret_codes.append(secret_code)
return secret_codes
poem = """Stars and the moon
shine in the sky
white and
until the end of the night"""
poem2 = 'The grass is green\nhere and there\nhoping for rain\nbefore it turns yellow'
poem3 = 'There\nonce\nwas\na\ndragon'
Try to rewrite the instructions as a series of steps.
For example,
Finally, return secret_codes, then uncomment the function call and update the argument to be [poem, poem2, poem3]. Now your function works with multiple poems at once, and can extract multiple pins!
return secret_codes
uncomment the function call
update the argument to be [poem, poem2, poem3]
You did all three of these things. So, you DO actually understand the instructions perfectly. It was done.
Then, you changed the function parameters to require 3 arguments. That wasn’t in the instructions, so don’t do that.
It also does not make sense since you call the function with 1 argument, a list.
Then you rearranged the code and removed some variable declarations. Also, not in the instructions.