What's wrong with this code?

I tried out this code and it said there was invalid syntax where the second new_list.append appears

def openOrSenior(data):
    # Hmmm.. Where to start?
    new_list = []
    for piece in data:
        if piece[0] >= 55 and piece[1] >= 7:
        else new_list.append('Open')
        return new_list

You’re missing a colon : after else. :wink:


Is there a python text editor on codecademy which I can use where I can test out any code?

This site has an editor which lets you see your code execute step by step. It has some limitations, but works well for testing most Codecademy exercises.
Visualize Python, Java, JavaScript, C, C++, Ruby code execution


Hey, thanks for the link.

But I just tried the above code on it and for some reason it only executes one step. Any ideas?

Because you return inside the loop. Remove 4 spaces from the beginning of your return statement.


should it be aligned with “for”

I fixed the return statement. You can try it for yourself

Yes, aligned with the for. All good now?

1 Like

The code works, but the link I tried it at still only does one step after the return fix

Can you post your latest code?

Its the same code used on this link:

Visualize Python, Java, JavaScript, C, C++, Ruby code execution

I don’t see any code there. Can you copy and paste your code in this thread?

That link just goes to the page when you can run the code, it doesn’t actually have your code on it.

Can you post it to the forum, or use the “Generate permanent link” button on that site to show it to us? :slight_smile:

I thought you guys were going to copy and paste the code into the link. Anyways here is the perm link:

http://www.pythontutor.com/visualize.html#code=def%20openOrSenior(data)%3A %20%20%20%20new_list%20%3D%20[] %20%20%20%20for%20piece%20in%20data%3A %20%20%20%20%20%20%20%20if%20piece[0]%20>%3D%2055%20and%20piece[1]%20>%3D%207%3A %20%20%20%20%20%20%20%20%20%20%20%20new_list.append(‘Senior’) %20%20%20%20%20%20%20%20else%3A%20new_list.append(‘Open’) %20%20%20%20%20%20%20%20 %20%20%20%20return%20new_list&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=[]&textReferences=false

The code looks fine, but since I don’t know what value is passed in for the data parameter, I can’t tell if should loop more than once or not. Also, is data expected to be a list or a dict?

1 Like

It should be a list. Yeah, I never passed in argument

1 Like

Then what makes you think it doesn’t work?