-4
user = input("userinput: ")

myList = ["Noah", "Mike"]
for item in list:
    if user == item:
        print("hello {}".format(user))
    else:
        print("invalid")

what I am trying to do is if you said something in the list you get hello (and the name you pick)

for ex:

input: Noah

output: hello Noah

And if you do not put any thing in nothing in the list correctly

input: Mikee

output: invalid

so basically I am asking a more complex if and else statement in python lists

zenwraight
  • 2,002
  • 1
  • 10
  • 14
  • 1
    One error is that the name of your `list` is `myList` so it should be `for item in myList:` – quantik Sep 27 '17 at 15:59
  • 3
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Chris_Rands Sep 27 '17 at 16:00

3 Answers3

0

You're iterating over each item in the list and printing its validity. You want to check just once:

user = input("userinput: ")

myList = ["Noah", "Mike"]
if user in myList:
    print("hello {}".format(user))
else:
    print("invalid")
Cuber
  • 713
  • 4
  • 17
0
user = input("userinput: ")

myList = ["Noah", "Mike"]

if user in myList:
    print("hello {}".format(user))
else:
    print("invalid")

you need to just check that value is exist in list or not.

Dharmesh Fumakiya
  • 2,276
  • 2
  • 11
  • 17
0

Seems like a great use case for ternary operator:

user = input("userinput: ")
myList = ["Noah", "Mike"]

print("hello {}".format(user)) if (user in myList) else print("invalid")
g-bear
  • 11
  • 4