0

I have just started to learn python and am trying to solve a basic elemenatary problem.

I would like to ask the user to input their name. There are only two valid names (Bob and Alice) that should receive a greeting. Any users who do not go by those names should receive an incorrect entry message.

I have created a list of names which include Bob and Alice. I have added a code to prompt the user to input their name.

I have written a print statement which greets the user after inputting the name.

The difficulty I am having is knowing which function I should use to invalidate any users that aren't Bob and Alice. I have written the following code;

    names = ("Alice", "Bob", "Ray")

    name = input("What is your name?")
    if name == "Alice" "Bob":
        print("Hello", name)
    else:
        print("Invalid entry")

My code is only ending in invalid entry, regardless of which name is used.

Sugar Ray
  • 3
  • 1
  • Does this answer your question? [Checking multiple values for a variable](https://stackoverflow.com/questions/15851146/checking-multiple-values-for-a-variable) – Henry Harutyunyan Jun 23 '20 at 23:14

1 Answers1

0

The problem is here:

if name == "Alice" "Bob":

Python concatenates strings that are one after the other with nothing in between, so it effectively checks whether the name is "AliceBob". Just check what it prints if you type it.

What you want to do is a check like this:

if name == "Alice" or name == "Bob":

or like this:

if name in ("Alice", "Bob"):
kszl
  • 1,203
  • 1
  • 11
  • 18