0

I'm trying to create an operator where the user types in the position on the chessboard and my program will print out if it's standing on a black or a white square. The problem is that my if statement will not run through my arrays properly.

pos = input (f'Hvilken posisjon står brikken på? ') 

bokstav = pos[0]                

tall = int(pos[1])              

for x in bokstav:
  for y in str(tall):
    
    partall_svart = ['b','d','f','h']
    partall_hvit = ['a','c','e','g']

    print (partall_svart)
    print (partall_hvit)
    
    if x == partall_svart and int(y) % 2 == 0:
        print (f'Posisjon: {bokstav}{tall}\nSvart')
    
    elif x == partall_svart and int(y) % 2 == 1:
        print (f'Posisjon: {bokstav}{tall}\nHvit')
    
    if x == partall_hvit and int(y) % 2 == 0:
        print (f'Posisjon: {bokstav}{tall}\nHvit')
    
    elif x == partall_hvit and int(y) % 2 == 1:
        print (f'Posisjon: {bokstav}{tall}\nSvart')
  • 2
    Which `if` statement do you mean? Are you trying to check whether `x` is one of the items in `partall_svart`? Use `in`, not `==`. – mkrieger1 Sep 16 '21 at 10:58
  • 2
    This question is answered here: [Check if item is in an array / list](https://stackoverflow.com/questions/11251709/check-if-item-is-in-an-array-list) – mkrieger1 Sep 16 '21 at 11:01
  • The code-point of `aA` is an odd number. So, since the square `A1` is black, all you need to do is check whether the parity of the row-number is the same as the parity of column-letter code-point: i.e. `svart = ord(pos[0]) % 2 == int(pos[1]) % 2`. The for-loops aren't needed. – ekhumoro Sep 16 '21 at 11:46

1 Answers1

0

Actually you could just make a number from the letter part too, and then use %2 on both parts. As ekhumoro's suggestion with ord() may be a bit esoteric, this code uses find() instead:

pos = input (f'Hvilken posisjon star brikken pa? ')

bokstav = pos[0]                
bokstavtall = "abcdefgh".find(bokstav.lower()) + 1

tall = int(pos[1])              

print (f'Posisjon: {bokstav}{tall}\n{"Svart" if tall % 2 == bokstavtall % 2 else "Hvit"}')
tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • Has the world really become so dumbed-down now that learning what a simple function like [ord](https://docs.python.org/3/library/functions.html#ord) does is considered too onerous? I could also have suggested `int(pos[0], 18) - 9` - but presumably that would be considered even more "esoteric" as it implies an elementary understanding of number bases. – ekhumoro Sep 16 '21 at 17:07
  • @ekhumoro I like the 18-base more, to be honest. Remembering ASCII codes is just less intuitive. – tevemadar Sep 16 '21 at 17:11
  • Surely the whole point of `ord` is to save you the trouble of remembering the code-points? But in any case, it's the parity that matters, not the exact numbers. – ekhumoro Sep 16 '21 at 17:20
  • Remembering 1 bit of an ASCII code is for sure something what people do all the time :-) – tevemadar Sep 16 '21 at 18:01
  • I'm referring to the [mathematical parity](https://en.wikipedia.org/wiki/Parity_(mathematics)), which simply means the oddness/evenness of an integer. I think most people know that `65` and `97` are odd numbers, and can make the reasonable assumption that the relevant code-points are sequential ;-) – ekhumoro Sep 16 '21 at 18:18