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;
}







0
















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










share|improve this 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.

























    0
















    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










    share|improve this 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.





















      0












      0








      0









      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










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      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.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          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






          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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






            share|improve this answer




























              1














              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






              share|improve this answer


























                1












                1








                1







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 2:18









                AetherUnboundAetherUnbound

                94369




                94369

















                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents