1

I am trying to write a program about poker game and for the comparison part I called (returned) multiples values for each set up.

In this case, I returned an array, one of them was a boolien and one of them was an integer. But when I tried to use them in another function I get an error ('bool' object is not subscriptable) and I dont know why. My whole code is nearly 150 line and for running it you ll need extra file so I ll share some parts of it.

these are the set up parts for each combination

def straight(n):
    F = converter(n)
    if consecutive(F) == True:
        return [True, F[0]]
    return False


def full_house(n):
    F = converter(n)
    if len(set(F)) == 2:
        for i in F:
            if F.count(i) == 3:
                return [True, i]
    return False

this is the part where I will rank them

def ranking(n, k):
    if n == "AKQJT" and flush(k) == True:
        return 9
    elif  straight(n)[0]== True and flush(k) == True:
        return [8,straight(n)[1]]

    elif four_kind(n)[0]== True:
        return [7,four_kind(n)[1]]

    elif (full_house(n))[0]== True:
        return [6,full_house(n)[1]]

    elif flush(k) == True:
        return 5

    elif (straight(n))[0]== True:
        return [4,straight(n)[1]]

for example when I try

print(ranking("44447","DDDDD"))

I get an error

 elif  straight(n)[0]== True and flush(k) == True: line ...
 TypeError: 'bool' object is not subscriptable

But interstingly when I try the straight flush (the second elif part tests it). For example,

print(ranking("23456","DDDDD")

I get an answer like

[8,6] 

which its the true answer but then again I get the same error.

3
  • Because you're returning two different types, you can't assume you'll always get an array. Why not just return an array with a None in the False case? Commented Aug 24, 2018 at 22:13
  • it looks like straight(n) returns a boolean value and you are trying to index it with [0]. Indeed straight may return False. Commented Aug 24, 2018 at 22:14
  • pass a pointer reference for them in the parameters of the functions Commented Aug 24, 2018 at 22:24

2 Answers 2

2

In the default case, you do not return an array:

    return False

Change it to something like

    return [False, None]

or whatever makes sense for your situation.

Sign up to request clarification or add additional context in comments.

3 Comments

I thought that if its false then it would just pass that line and jump to another elif I never thought this actually. It seems like problem is solved
Realistically, the whole function should just be changed to return i and return None instead of a pair at all, but that’s outside of the scope of the question.
That makes much more sense..I ll try that
1

Please checkout what you are returning through straight(n). I believe in this case you are trying to return False. So, Boolean is not subscript-able.

If you get get straight(n) as False. You cannot write if-elif conditions to verify their cases. You need to design nested loops for cases straight(n) is False and straight(n) is not equal to False.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.