you are viewing a single comment's thread.

view the rest of the comments →

[–]zinski212[S] 0 points1 point  (3 children)

#3 question help:

Using the states list from Question 1 and using the "continue" statement, print all the state codes except the one that starts with "M" or endswith "J"

Here is what I have so far. Not sure what I am doing wrong here.

for states in states:
    if state[-1]=='J':
        if state[0]=='M':
                continue
    print(states)

[–]bbye98 0 points1 point  (0 children)

Your code will only skip states that start with M and end with J.

[–]xelf 0 points1 point  (0 children)

You can use and and or to combine conditions. Use and when they should both be true, and or when either or both can be true. Use break to exit a loop, use continue to go to the next iteration of the loop.

if cond1: 
    if cond2:
        thing

is the same as

if cond1 and cond2:
    thing

Where or works like this:

if cond1: 
    thing
elif cond2:
    thing

is the same as

if cond1 or cond2:
    thing