For loop with list

I’m new to python and following a book to learn it. However there are some exercises without any solution, so i thought I ask it here, see if you can help me out.

First I need to make a list of names:
sailors = [“Jan”, “Piet”, “Joris”, “Korneel”]

Then I need to ask the user to input his/her name:
name = input (“Type your name please”.)

Then, and HERE is my problem, I need to use a for loop to check if the name is in the list.
If so, I need to print an answer like ("Hi, welcome’)

So far I came up with this:

sailors = [“Jan”, “Piet”, “Joris”, “Korneel”]
name = input (“What is your name?”)
for name in sailors:
if (name == “Jan” ):
print (“Well you must be a sailor!”)
print (“You are not a sailor”)

But if I run it and I type in “Jan”, the output is:
well you must be a sailor
you are not a sailor

So, with just this basic commands, hope you can help me out to understand how this works.

Rather than the for loop and if (name == "Jan" ): try if name in sailors:

I’ll let you work out the rest, but post back if you get stuck.

Changed it but same output, says four times ‘you must be a sailor’ and one time ‘you are not’;

Drop the for loop.

But for the exercise I need the for loop, that’s what I think is so weird. I can write a simple script that looks for the name.

Ah, I see. In that case don’t use name for both the input and the for loop; use something like input_name =...

Hey man, thank you, solved it with your help. I also added break to stop the loop after the name.

1 Like

In the future, post blocks of code between triple backtick lines:

<code lines>

Note that indents disappeared as originally posted. You can edit your post by clicking the pencil icon beneath it.