0
availableNums=["one","two","three","four","five"]
selectedNumbers=[]
for value in range(0,3):
    selectedNumbers.append(raw_input("Choose a number:"))

if selectedNumbers not in availableNums:
      print("The number "+str(selectedNumbers)+" isn't available/nBut it will be changed to six")

When I run this code and it asks for the numbers, I type in the numbers that are available in the list, but it still says that "The number ['one','two','three'] isn't available But it will be changed to six". Why is it doing that?

I think that I have to change the not or in part but I am not sure.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

2 Answers2

1

in checks membership: a in b is true if and only if a is a member contained by b.

Since availableNums is a tuple of ints, selectedNumbers, which is a list, isn't a member. You seem to be wanting to check whether selectedNumbers is a subset of availableNums.

You can either check each item in a loop:

for s in selectedNumbers:
    if s not in availableNums
        ....

Or you can convert them to sets, if you're okay with checking all at once and failing completely if any of the selected numbers are invalid:

if not set(selectedNumbers) < set(availableNums):
    ....

Note that < here, applied to sets, is the subset operator.


Also, as noted in a comment, raw_input returns a string, but you're attempting to treat it as an integer. You can use int() to parse the input string.

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
  • Thank you that worked! but what if the availableNums was a string not a tuple how would you do that? – John Cycler Dec 13 '17 at 00:10
  • @JohnCycler I'm not sure what you mean. Are you asking what to do if you have `availableNums = "some_string"` and you want to check whether the user-entered string `rawNumbers` is comprised of a subset of the letters in `some_string`, or...? – Kyle Strand Dec 13 '17 at 01:28
  • @JohnCycler Based on your edit, it looks like you've turned `availableNums` into a *list of strings*, not just a *string*. In this case, you obviously won't use `int()` to convert your `raw_input` value into an integer for comparison, but otherwise my answer would remain unchanged. – Kyle Strand Dec 13 '17 at 18:03
0

selectedNumbers is a list. You are checking if the whole list is in availableNums, not if each number in selectedNumbers is in availableNums.

It sounds like you want something like:

for selectedNumber in selectedNumbers:
    if selectedNumber not in availableNums:
        # selectedNumber from selectedNumbers is not in availableNums

Edit: As hatshepsut pointed out, your code places strings in selectedNumbers, not integers. Use input instead of raw_input1 or convert to int.

1 For Python 2. For Python 3, see What's the difference between raw_input() and input() in python3.x?

Galen
  • 1,307
  • 8
  • 15
  • Thank you that worked! but what if the availableNums was a string not a tuple how would you do that? – John Cycler Dec 13 '17 at 00:11
  • @JohnCycler A single string representing multiple numbers? One way would be to split the string into string representations of each number and then convert them to ints. – Galen Dec 13 '17 at 00:14
  • So now I edited my post by making some changes with the availableNums. Can you please look at that – John Cycler Dec 13 '17 at 00:20