Help with Lab (Writing a IF statement inside of a FOR loop)

Here is the assignment exactly. I understood everything until we got to this section…I need help grasping it.

Assign allow_list to a list of IP addresses that are allowed to log in

allow_list = [“”, “”, “”, “”,
“”, “”, “”, “”]

Assign ip_addresses to a list of IP addresses from which users have tried to log in

ip_addresses = [“”, “”, “”, “”,
“”, “”]

For each IP address in the list of IP addresses from which users have tried to log in,

If it is among the allowed addresses, then display “IP address is allowed”

Otherwise, display “IP address is not allowed”

for### YOUR CODE HERE ###
if i in ### YOUR CODE HERE ###:
print(“IP address is allowed.”)
print(“IP address is not allowed.”)

Please read the pinned message for guidance on how to format your code. It’s very hard to help you debug code when there’s no formatting.

It’s quite straightforward: for each IPA in ip_addresses, you need to check if the IPA is in the allow_list and print “IP address is allowed.”, else: you print “IP address is not allowed.”

Short of writing the code for you (which I’ve almost done) that’s about as much help as I can offer.

Give it a go and post back your code if you get stuck. As already mentioned, please post your code in a formatted code block.

What exactly do you think you don’t understand about it?

For example, what kinds of syntax do you think the assignment is expecting you to use (according to the course notes, the assignment itself, what was talked about in the previous lessons…)? Do you know how to use them? Have you tried using them for other things? Is there something specific that you found confusing?

1 Like

I don’t understand how to use the for loop with if…

Since the variables “ip_addresses” and “allow_list” are defined it makes sense for it to be

If ip_addresses in allow_list:
print(“ip address is allowed”)
print(“ip address not allowed”)

I know for is a loop like while just can’t figure out how to use it here.

I know it could be used like:
For i in range (0,10)
print (i)

I know it’s super easy but I have to grasp it to unlock the confusion.

You’re close.

Where you know how to do for i in range(): you simply swap out range() for your list object’, like this…

ip_addresses = ["", "", ""]

for IPA in ip_addresses:

Now, Python will ‘iterate’ over said list and the variable, IPA, will hold each item in turn, rather than i:




So, all you need to do is to check: if IPA in allow_list: and Python will check each item in the allow_list and return True if an exact match is found, or False if no match is found. Those two returns are what the if/else branch uses to determine which branch of code will be executed.

Do you see?

Thank you, Rob. I do see. It took me literally all day to understand but this was the end result…

> for i in ip_addresses:
>     if i in allow_list:
>         print("IP address is allowed.")
>     else:
>         print("IP address is not allowed.")

Reading it like this sounds like basic English. It stumped me though. Maybe I’m overthinking it.

You’re welcome.

Just one of the many virtues with which Python is blessed.

Happy coding.