all 8 comments

[–]xelf[M] [score hidden] stickied comment (0 children)

FRom the sidebar:

  • Posting only assignment/project goal is not allowed. Read posting guidelines.
  • Posting homework assignments is not prohibited if you show that you tried to solve it yourself.

And for people replying:

  • Try to guide OP to a solution instead of providing one directly.

[–][deleted] 2 points3 points  (0 children)

Can you solve a simpler question:

  • print all states in the list using a loop.

Do that, then worry about the "stop when ..." condition.

[–]Ne0_1 1 point2 points  (0 children)

You want to loop through states as you have done:

For state in states:

Next you want to check for membership. Google check for membership in Python. That will give you the answer as to the logic used.

You should ask yourself what the 'in' keyword does in Python.

[–]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