0

As per title, I am trying to check if an input string is either 'white' or 'black', if not to raise an exception.

I came up with the following:

class Player:
    def __init__(self):
        self.name = input('Enter you Name: ')
        colour = input('Now choose your army colour between "white" and "black": ')
        if colour is not 'white' or colour is not 'black':
            raise Exception('colour must be either "white" or "black"')
        else:
            self.colour = colour

    def get_name(self):
        return self.name

    def get_colour(self):
        return self.colour


player = Player()

Which looks ok to me, but when I type 'white' or 'black' it rises the exception anyway.

C:\Users\oricc\PycharmProjects\practisingDrowing\venv\Scripts\python.exe C:/Users/oricc/PycharmProjects/practisingDrowing/canvas.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Enter you Name: John
Now choose your army colour between "white" and "black": white
Traceback (most recent call last):
  File "C:/Users/oricc/PycharmProjects/practisingDrowing/canvas.py", line 67, in <module>
    player = Player()
  File "C:/Users/oricc/PycharmProjects/practisingDrowing/canvas.py", line 56, in __init__
    raise Exception('colour must be either "white" or "black"')
Exception: colour must be either "white" or "black"

Process finished with exit code 1

Where is the error?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Techoplite
  • 605
  • 9
  • 23
  • 2
    If color is white -> then it is not black. And if color is black -> then it is not white. So your condition for the exception is always True. You want `and` instead of `or` – Tomerikoo Oct 30 '19 at 16:14
  • Not sure why you're getting downvoted. This is a beginner issue for sure, but that's not a crime. As far as I can tell, you have a complete description of a proper programming question. +1 for following site rules. – Mad Physicist Oct 30 '19 at 16:47
  • No problem, as long as I know the difference between is and == .. although I noticed most of the time works fine. I genuinely thought they where the same but now I will use them more carefully. Anyway the problem was the 'or' that should have been 'and'. I thought I was fine with logic statements but I was not. Only now I am getting the grasp... – Techoplite Oct 31 '19 at 18:29
  • 1
    While it seems your issue with string comparison has been fixed, I would strongly consider using an Enum instead like Color.BLACK and Color.WHITE. It is more verbose, but comparisons would be quicker and there would be less concerns for validating against typos, unlike strings. – CodeSurgeon Oct 31 '19 at 23:32

5 Answers5

4

Your issue is at if colour is not 'white' or colour is not 'black':

is is for identity testing, while == is for equality testing. Use is if you want to compare two objects to see if they're the same object; use == if you want to compare their values instead.

if colour is not white will return true unless colour is literally the object string 'white'. if colour != 'white' compares the values instead, and is what you want to be using here.

(Also note that you should be using and instead of or - the string can't be 'black' and 'white' at the same time.)

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Nick Reed
  • 4,989
  • 4
  • 17
  • 37
2

colour will always be either not 'white' or not 'black'. You should use the and operator instead of or. Also note that the is not operator is meant for identity test. To test for value inequality you should use the != operator instead:

if colour != 'white' and colour != 'black':
blhsing
  • 91,368
  • 6
  • 71
  • 106
2

Change the line

if colour is not 'white' or colour is not 'black':

to

if colour != 'white' and colour != 'black':
Groger
  • 532
  • 3
  • 15
1

It will always be true, colour cannot be both black and white at the same time, you can just use in

if colour not in ['white', 'black']:
Sayse
  • 42,633
  • 14
  • 77
  • 146
1

You should replace:

if colour is not 'white' or colour is not 'black':

with:

if colour.lower() not in ['white', 'black'] :
lenik
  • 23,228
  • 4
  • 34
  • 43