-1

If the 'choice' valiance contains 'a','b','c' at the list each character link a number ('1','2','3').

For example choice = ['a','b','c'] links the numbers '1','2','3'.

choice = ['a','b','c']

def select(choice):
   if choice == ['a']:
      answer = '1'
   elif choice == ['b']:
      answer = '2'
   elif choice == ['c']:
      answer = '3'
   elif choice == ['a', 'b']:
      answer = "'1', '2'"
   elif choice == ['a', 'c']:
      answer = "'1', '3'"
   elif choice == ['b', 'c']:
      answer = "'2', '3'"
   else
      answer = "'1', '2', '3'"

Could I simply make it using another method?

SJJ
  • 25
  • 3
  • Sorry, "valiance"? That seems to be a typo. I think you meant "variable". – wjandrea Nov 20 '22 at 20:53
  • What do you mean by "another method"? I'm not sure if you're using "method" to mean "function" or "technique". Either way, what other method would you want to use? Please [edit] to clarify, and see [ask] for tips. – wjandrea Nov 20 '22 at 20:54
  • 1
    What does this have to do with probability? You might mean "possibilities" instead. – wjandrea Nov 20 '22 at 21:00
  • I think that - despite the typos - it's pretty clear what the OP is asking; can one do-away with the tedious (and non-scalable) if-elif-elif-elif-else structure, to get the "answer" from the "choice" in a more concise and simple manner. – ShlomiF Nov 20 '22 at 21:00
  • @ShlomiF Fair enough, although there are existing questions about that, for example: [Replacements for switch statement in Python?](/q/60208/4518341) – wjandrea Nov 20 '22 at 21:02
  • I think the "using a dict as an alternative to switch-case" is much more general than the simple use-case here. And also slightly dfferent in that one wants all the combinations, which means that using the method you linked would require creating a dict with all subsets as keys, which is really overkill for this simple problem in my opinion. So this warants a separate answer as a separate question. – ShlomiF Nov 20 '22 at 21:05
  • @ShlomiF Please don't change what OP's saying, and don't edit their code (except for readability). I rolled back your edit. Let them clarify it themselves. – wjandrea Nov 20 '22 at 21:07
  • Clarifying the question is not a drastic change. Especially if it's mainly linguistic mistakes. But ok. – ShlomiF Nov 20 '22 at 21:08

1 Answers1

2

You can map the choices to their values with a dictionary, and then just use a list-comprehension to get the answer from the choice:

choice2val = {'a': '1', 'b': '2', 'c': '3'}
def select(choice):
    answer = [v for k, v in choice2val.items() if k in choice]
    return answer

choice = ['a', 'c']  # example     
print(select(choice))
# ['1', '3']
ShlomiF
  • 2,686
  • 1
  • 14
  • 19