0

In Python... In the following code, the user's input causes one of 5 lists to be printed. Is there a way to make one line that is something like:

print(", ".join(EmpX_list))

Where "X" could be the value of the user input, emp_num in this case? That way it is one line and not 5.

emp_num = int(input('Enter Employee Number:'))
if emp_num==1:
    print(", ".join(Emp1_list))
if emp_num==2:
    print(", ".join(Emp2_list))
if emp_num==3:
    print(", ".join(Emp3_list))
if emp_num==4:
    print(", ".join(Emp4_list))
if emp_num==5:
    print(", ".join(Emp5_list))

Thanks in advance!

Neil
  • 493
  • 1
  • 8
  • In other words, a single print command would be "constructed" in part by the user's input. The could be no 'if's at all in that case. – Neil May 15 '22 at 18:09
  • 2
    See e.g. https://stackoverflow.com/q/1373164/3001761, don't do this. – jonrsharpe May 15 '22 at 18:09
  • Yes, I've seen the warnings about variable variables. In my imagination: The variable in the print statement would be a dedicated one, for that purpose, and could be 'reset' to it's initial state immediately after printing. It is not changing the name of any list that is there, or any variable that would be used anywhere else... only building a print statement...In other words, one pass through this part of the code, then it can go back to some aribitrary value, or even not, since another pass through would give it whatever value it needed to print the desired list at that time. – Neil May 15 '22 at 18:14
  • 1
    Your comment doesn't make any sense. How is that distinct from what I've linked? Where do these lists come from, why are they constructed like that to start with? – jonrsharpe May 15 '22 at 18:17
  • The lists come from user entry and are appended by those entries. I've read the link you sent before. I dont want to make a variable variable, I want a variable in a command. Print listX where X is a user input. Conceptually, it would be inserting the name of the list into the print statement according to the user's input. Who knows. Yeah my mind is a bit abstract. Thanks for the replies. EDIT: I guess the problem is 'addressing' a list with a number. If the user enters 4 for employee 4, what is to associate 4 with emp4_list. Hmmm....getting some ideas. More coffee. – Neil May 15 '22 at 18:27
  • 1
    You want a variable in a variable that's being _passed_ to that "command" (actually a function being called with the result of a method call). As shown below the same techniques (just use a dictionary) work here. And if you read the probable dupe, why is that not discussed _in the question_? – jonrsharpe May 15 '22 at 18:31

1 Answers1

1

You can print exact list without using if-else statements, but then you'll need to use python dictionary. Just store your lists with corresponding number as key value pair in dictionary and just access the element of the key user enters.

So your code will look like:

Employees = { 1: Emp1_list, 2: Emp2_list, 3: Emp3_list, 4: Emp4_list, 5: Emp5_list}

emp_num = int(input('Enter Employee Number:'))
print(','.join(Employees[emp_num]))

Hope this answers your question. You can also refer the answer to the question jonrsharpe mentioned in comment.

Jake Peralta
  • 408
  • 3
  • 9
  • `emp_num.get(emp_num)` doesn't really make sense... Please test your code before posting it! – Thierry Lathuille May 15 '22 at 18:21
  • ohh damnn! sorry mate, posted wrong variable name in hurry. – Jake Peralta May 15 '22 at 18:24
  • 1
    Note that it would be much better to use `Employes[emp_num]`. In case the number is invalid, this would immediately raise an explicit `KeyError`. `Employees.get(emp_num)` would return `None` in this case, causing `join` to fail with a less explicit `TypeError: can only join an iterable` – Thierry Lathuille May 15 '22 at 18:29
  • Yeah right, even giving default value to get method seems better option – Jake Peralta May 15 '22 at 18:34
  • If I request Employee number 10 and silently get the info of Employee 1, I would not be so happy... Better to fail loud than push wrong data further... – Thierry Lathuille May 15 '22 at 18:37
  • A default value is most likely _worse_, ["Errors should never pass silently."](https://peps.python.org/pep-0020/#the-zen-of-python) – jonrsharpe May 15 '22 at 18:39
  • Can't deny that! Better leave error handling on OP than providing false value. Will refactor the answer for same – Jake Peralta May 15 '22 at 18:40