Tuple values not being accurately compared in game logic [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How to test multiple variables against a value?
21 answers
I am refactoring code for a Tic-Tac-Toe application in Python however i am running into trouble when rewriting the function that checks for a winning condition.
This is what i have right now
x = ('X', 'X', 'X')
o = ('O', 'O', 'O')
if ('X', '-', '-') == o or x:
print(True)
This returns True even though the string shown is clearly not either one of the ones it's been compared to. However what is even weirder is that when I compare it to just one tuple
if ('X', '-', '-') == o:
print(True)
True doesn't get returned. Can someone please explain why this happens
python boolean tuples
marked as duplicate by Daniel Pryden, Community♦ Nov 23 '18 at 2:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to test multiple variables against a value?
21 answers
I am refactoring code for a Tic-Tac-Toe application in Python however i am running into trouble when rewriting the function that checks for a winning condition.
This is what i have right now
x = ('X', 'X', 'X')
o = ('O', 'O', 'O')
if ('X', '-', '-') == o or x:
print(True)
This returns True even though the string shown is clearly not either one of the ones it's been compared to. However what is even weirder is that when I compare it to just one tuple
if ('X', '-', '-') == o:
print(True)
True doesn't get returned. Can someone please explain why this happens
python boolean tuples
marked as duplicate by Daniel Pryden, Community♦ Nov 23 '18 at 2:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to test multiple variables against a value?
21 answers
I am refactoring code for a Tic-Tac-Toe application in Python however i am running into trouble when rewriting the function that checks for a winning condition.
This is what i have right now
x = ('X', 'X', 'X')
o = ('O', 'O', 'O')
if ('X', '-', '-') == o or x:
print(True)
This returns True even though the string shown is clearly not either one of the ones it's been compared to. However what is even weirder is that when I compare it to just one tuple
if ('X', '-', '-') == o:
print(True)
True doesn't get returned. Can someone please explain why this happens
python boolean tuples
This question already has an answer here:
How to test multiple variables against a value?
21 answers
I am refactoring code for a Tic-Tac-Toe application in Python however i am running into trouble when rewriting the function that checks for a winning condition.
This is what i have right now
x = ('X', 'X', 'X')
o = ('O', 'O', 'O')
if ('X', '-', '-') == o or x:
print(True)
This returns True even though the string shown is clearly not either one of the ones it's been compared to. However what is even weirder is that when I compare it to just one tuple
if ('X', '-', '-') == o:
print(True)
True doesn't get returned. Can someone please explain why this happens
This question already has an answer here:
How to test multiple variables against a value?
21 answers
python boolean tuples
python boolean tuples
asked Nov 23 '18 at 2:13
LekeLeke
255
255
marked as duplicate by Daniel Pryden, Community♦ Nov 23 '18 at 2:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Daniel Pryden, Community♦ Nov 23 '18 at 2:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Okay let's break this down. The way you have your code written, you have two things you're comparing to see if they're "truthy":
('X', '-', '-') == o
x
The half of your or
conditional is x
itself. Since a tuple with values is considered truthy, your condition (everything after if
) will always evaluate to true! If you're trying to compare ('X', '-', '-')
to o
and to x
, you'll need to do this:
if ('X', '-', '-') == o or ('X', '-', '-') == x
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Okay let's break this down. The way you have your code written, you have two things you're comparing to see if they're "truthy":
('X', '-', '-') == o
x
The half of your or
conditional is x
itself. Since a tuple with values is considered truthy, your condition (everything after if
) will always evaluate to true! If you're trying to compare ('X', '-', '-')
to o
and to x
, you'll need to do this:
if ('X', '-', '-') == o or ('X', '-', '-') == x
add a comment |
Okay let's break this down. The way you have your code written, you have two things you're comparing to see if they're "truthy":
('X', '-', '-') == o
x
The half of your or
conditional is x
itself. Since a tuple with values is considered truthy, your condition (everything after if
) will always evaluate to true! If you're trying to compare ('X', '-', '-')
to o
and to x
, you'll need to do this:
if ('X', '-', '-') == o or ('X', '-', '-') == x
add a comment |
Okay let's break this down. The way you have your code written, you have two things you're comparing to see if they're "truthy":
('X', '-', '-') == o
x
The half of your or
conditional is x
itself. Since a tuple with values is considered truthy, your condition (everything after if
) will always evaluate to true! If you're trying to compare ('X', '-', '-')
to o
and to x
, you'll need to do this:
if ('X', '-', '-') == o or ('X', '-', '-') == x
Okay let's break this down. The way you have your code written, you have two things you're comparing to see if they're "truthy":
('X', '-', '-') == o
x
The half of your or
conditional is x
itself. Since a tuple with values is considered truthy, your condition (everything after if
) will always evaluate to true! If you're trying to compare ('X', '-', '-')
to o
and to x
, you'll need to do this:
if ('X', '-', '-') == o or ('X', '-', '-') == x
answered Nov 23 '18 at 2:18
AetherUnboundAetherUnbound
94369
94369
add a comment |
add a comment |