jeudi 16 septembre 2021

Array in python in nested for-loops

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')

Aucun commentaire:

Enregistrer un commentaire