-2

I'm currently learning python and doing some basic tasks but I'm having difficulties with this code:

names = ["Terry", "Ben", "Mike"]
name = input("oya enter name ")
if names == name:
     print("Welcome " + name)
else:
     print("Who are you???!!!")

I'm trying to get the user to input a name and if the name is not in the array the code will print the else command but when I run the code and enter one of the names it does not work. Any solution please?

tadman
  • 208,517
  • 23
  • 234
  • 262
T1OG
  • 5
  • Please repost your code with proper formatting. Read https://stackoverflow.com/help/formatting – Barmar Feb 28 '23 at 00:40
  • 5
    `if names == name:` should be `if name in names:` – Barmar Feb 28 '23 at 00:41
  • 1
    Does this answer your question? [Fastest way to check if a value exists in a list](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list) – Pranav Hosangadi Feb 28 '23 at 00:48

1 Answers1

1
names = ["Terry", "Ben", "Mike"]
name = input("oya enter name ")
if name in names:
     print("Welcome " + name)
else:
     print("Who are you???!!!")

All you have to do is write name in names.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Sakshi Sharma
  • 133
  • 1
  • 8
  • 1
    Also, using f-strings might be easier to read than string concatenation: `print(f"Welcome {name}.")`. – chubercik Feb 28 '23 at 00:55