-3

Basically I am struggling to figure out how to best do an output statement based on the user input.

I want to run code that asks the user the name of a client and returns if it matches a certain name a particular piece of information and if the name is another name returns different information which I believe would be done via the ELIF statement but I am not sure and had issues running it like that before, any advice is appreciated...

client = input("Who is the client: ")
if client == "Jason":
    print("Age: 26, Work: Marketing, Office: LA")
aasoo
  • 100
  • 1
  • 8
  • 1
    Your question is not really clear. Can you add 2-3 examples of different input values and expected output values? You talk about different behaviours but only give an example of one. Which makes it hard to figure out what you need. – exhuma Sep 13 '20 at 19:55
  • Welcome back to SO! Please take the [tour] and read [ask]. It's not clear what you're asking, cause `elif` is exactly what you would use for that, though it's [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)er to [use a dict](https://stackoverflow.com/q/60208/4518341). Please [edit] to clarify. See also [mre]. – wjandrea Sep 13 '20 at 20:00
  • Your question still is not really clear but based on the following **if the name is another name returns different information** if you want a general case where `client` is not `Jason` and could be anything you use `else`, if you want a very specific case ex when `client` is `Jhon` then you would use `elif client=="Jhon"` – Countour-Integral Sep 13 '20 at 20:01
  • I just noticed you're missing a colon for the `if` statement, and the indentation is wrong. Is that what you're asking about? – wjandrea Sep 13 '20 at 20:12

1 Answers1

0

Instead of using a big series of if/elif's, I'd recommend creating a dictionary of people & doing it that way. It'll be easier to implement, look a lot cleaner & be faster overall.

clients = {
    "jason": {
        "age": 26,
        "work": "Marketing",
        "office": "LA"
        },
    # other clients go here
    }

client = input("Who is the client: ")
if client.lower() in clients:
    print(clients[client.lower()])

I used lower() on the names to make it case insensitive.

However, if you really do want to use if-statements:

if client == "Jason":
    # print Jason's info
elif client == "Dave":
    # print Dave's info
elif ...
wjandrea
  • 28,235
  • 9
  • 60
  • 81
stijndcl
  • 5,294
  • 1
  • 9
  • 23