0

I have three lists:

List_1 = [5, 34, 9, 24, 8, 16]
List_2 = [24, 1, 16, 9, 6, 30]
List_3 = [44, 20, 3, 4, 9, 19]

Let's say a user can input any number from 1-49.

I want a condition that: If the inputted number is present in List_1 but not in List_2 and not in List 3 - Print ("100")

Else, if the inputted number is present in List_1 and List_2 but not in List 3 - Print ("50")

Else, if the inputted number is present in List_1 and List_2 and List 3 - Print ("150")

Else, Print ("200")

Let's say the input is 16,
16 appears in 2 lists. This is what I did:

user_input = 16
List_1 = [5, 34, 9, 24, 8, 16]
List_2 = [24, 1, 16, 9, 6, 30]
List_3 = [44, 20, 3, 4, 9, 19]

if user_input in List_1 and not (List_2 and List_3):
   print("100")

elif user_input in (List_1 and List_2) and not List_3:
   print("50")

elif user_input in List_1 and List_2 and List_3:
   print("150")

else:
   print("200")

but the output is "150".

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
jeddy
  • 3
  • 2
  • Very similarly to [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/q/20002503/6045800), you have to repeat the `user_input in ...` for each list. `(List_2 and List_3)` simply returns one of the lists, not checking if the element is in them... – Tomerikoo Mar 01 '22 at 16:28

2 Answers2

1

Your checks are not doing what you think they are. You have to explicitly write the whole statement every time, so your first if statement for instance:

if user_input in List_1 and not (List_2 and List_3):

Should really be this:

if user_input in List_1 and not (user_input in List_2 and user_input in List_3):

It is important to make sure that your logical statements are truly doing what you think they are because before this fix your check was originally checking if the input is in List_1, and then checking that List_2 and List_3 are None/empty lists.

Your other if statements also have problems, and should be sorted out accordingly. It would probably be worth learning some more about logical statements.

1

Benjamin is correct that your logical comparators aren't grouping the way you think they might, that's the root of why you're getting incorrect values. From a readability/testability standpoint, I'd also consider refactoring the solution into something that is easier to understand:

List_1 = [5, 34, 9, 24, 8, 16]
List_2 = [24, 1, 16, 9, 6, 30]
List_3 = [44, 20, 3, 4, 9, 19]

def new(user_input):
  found = [False, False, False]      # List to contain results
  found[0] = user_input in List_1    # Update each item
  found[1] = user_input in List_2
  found[2] = user_input in List_3

  if found == [True, False, False]:  # Now explicitly look for 
    ret = 100                        # the pattern you're after
  elif found == [True, True, False]:
    ret = 50
  elif found == [True, True, True]:
    ret = 150
  else:
    ret = 200

  print('new: {0}'.format(ret))

def run():
  val = input("Enter a value:")
  new(int(val))

Long collections of and/or conditionals are a great spot to hide errors - especially when there seems to be little intuitive sense to the expected result.

EDIT: Per @Tomerikoo's comment below, this can be further simplified using list comprehension on the first part as such:

found = [user_input in l for l in (List_1, List_2, List_3)]
meshtron
  • 1,110
  • 9
  • 19
  • 1
    A bit simpler: `found = [user_input in l for l in (Lis1_2, List_2, List3)]` – Tomerikoo Mar 01 '22 at 16:29
  • @Tomerikoo definitely a bit simpler, but list comprehension is a couple chapters ahead yet. :) – meshtron Mar 01 '22 at 17:18
  • 1
    For you, or for the OP? xD If it's the OP you're concerned about, then don't be... We should strive to teach readers (remember this site is not just for answering the OP but for anyone coming here) about best practices and writing good code, not *"go easy on them"*. You could [edit] it as an alternative and maybe add a link for further reading if they don't understand it and care enough to read into it – Tomerikoo Mar 01 '22 at 17:26
  • @Tomerikoo for the OP. But, your point is valid that we should present the "best" way so I have edited and added the code and a link regarding list comprehension. – meshtron Mar 01 '22 at 19:12