0

this is for a tic tac toe game for my project but I still don't under stand the difference between my code and my mentor's code correct code:

def player_input():
    marker = ''

    while not (marker == 'X' or marker == 'O'):
        marker = input('Player 1: Do you want to be X or O? ').upper()

    if marker == 'X':
        return ('X', 'O')
    else:
        return ('O', 'X')

my code:

def player_input():
    player1 = ""
    while player1 != "X" or player1 !="O":
        player1 = input("choose x or o : ").upper()

    if player1 == "X":
       return ("X","O")
    else:
        return ("O", "X")

1 Answer 1

2

It's because your condition should have and and not or.

As per de morgans law:

~(p or q) => ~p and ~q

So your code should be as follows:


def player_input():
    player1 = ""
    while player1 != "X" and player1 !="O":
        player1 = input("choose x or o : ").upper()

    if player1 == "X":
       return ("X","O")
    else:
        return ("O", "X")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.