-2

I am writing a program that takes a blank input and does various things with it. The input "show me contact _____" (With the ____ being filled in with somebody's name). The names are stored in a list and I need it to be able to check if the name is in the list, and if it is, print the contact information. I figured an if statement would be the best way to do it, but I cannot figure out how.

Edit: I have tried using in, but my problem is that the input contains "show me contact _____" and not just the name. This causes this:

names = ['john doe','john doe','john doe']
user_input = input('>')
if user_input in names:
    print(email)

to not work.

ARol101
  • 63
  • 5

1 Answers1

3

You want

if user_input in names:

When used with a list, in checks for membership. When used with a string, it will check for substrings.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 2
    Or better still, `if user_input in set(names):`. Set should be made beforehand for it to make sense. – cs95 Jul 18 '17 at 19:35
  • 2
    A `set` is only (really) useful if you plan to do several queries or you can initialize it directly as set (`names = {'john doe','john doe','john doe'}`). – MSeifert Jul 18 '17 at 19:42