0

https://github.com/gabrirm/python-roulette-game.git

if betType == "red" and bet in red == True:
    self.balance += betprice[0] * 2
else:
    self.balance -= betprice[0]
    print("Oh, the ball landed on a black number :(. This is your current balance: ".format(self.balance))

if betType == "black" and bet in black == True:
    self.balance += betprice[0] * 2
else:
    self.balance -= betprice[0]
    print("Oh, the ball landed on a red number :(. This is your current balance: ".format(self. Balance))

So in this piece of code, when the user types 'red', why does the else statement keep executing, even though betType == 'red' and bet in red == True? both parts are true right? the else shouldn't execute.

3
  • 2
    What is red or black? This might be helpful: docs.python.org/3/reference/… Commented Aug 31, 2022 at 18:18
  • 2
    @KellyBundy I realized that was wrong, so edited my comment. Also, what I had before barely qualifies as an answer. Commented Aug 31, 2022 at 18:22
  • It might not have been nice as an answer, but you were clearly answering. Commented Aug 31, 2022 at 18:29

1 Answer 1

1

From documentation:

If A, B and C are operands and x is a comparison operator, then:

A x B x C is equivalent to:

(A x B) and (B x C)

You have this situation in this part: bet in red == True

So now this line:

betType == "red" and bet in red == True

is actually:

(betType == "red") and (bet in red) and (red == True)

From this table, both == and in are comparison operators.

If you want your condition to work as expected put parenthesis around the membership testing like: (bet in red) == True or better is to leave it like bet in red. That's it. No need to compare to True:

betType == "red" and bet in red
Sign up to request clarification or add additional context in comments.

1 Comment

You are right, but I wrote that in an attempt to solve the issue, I previously had: and bet in red', but didn't work either... I guess it is a syntax problem when getting the value of betType from the user's input. That's my shot, but I certainly can't identify the problem. Thank you so much anyway :)

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.