I want my variable to always be whole

Asked

Viewed 67 times

0

People need to get out of a drama where I can’t finalize my logic without getting stuck in condition, the code is part of an old game that I’m perfecting, my condition in the code below says that the user input value has to be NUMERIC and if it is numeric it has to be <= 8, if it is not numeric it enters the loop for the user to type again.

a = input('Jogador "X", onde pretende fazer a jogada? ')

    q = 1

    while q == 1:

        if a.isnumeric() and int(a) <= 8:
            a = int(a)
            break
        else:
            a = input('Jogador "X", digite um número de 0 a 8: ')

    if int(a) == int(b) or j[a] == 'X' or j[a] == 'O':

        while int(a) == int(b) or j[a] == 'X' or j[a] == 'O':   #essa linha é responsável pelo erro

            a = input('Jogador "X" por favor, escolha outro campo: ')

            if a.isnumeric() and int(a) <= 8:
                a = int(a)

            else:
                a = input('Jogador "X", digite um número de 0 a 8: ')

    j[a] = 'X'insira o código aqui
  • where variable B comes from and which error you are facing exactly?

  • then the full game is here https://repl.it/JxEi/4 type in the sequence of entries 1, 1, 1, ', ' ....... i want the filter not to pass if the user wants to type a quote or something other than numeric

1 answer

0


You need to make two whiles one inside the other. The first while will be to validate the move/move, and the second to validate the input. That is, for every move, you will first check the entrance and then if the field of the game is free.

This will result in something like this:

while c <= 10:
    if c % 2 == 1:
        a = input('Jogador "X", onde pretende fazer a jogada? ')
        while True:
            while True:
                if a.isnumeric() and int(a) <= 8:
                    a = int(a)
                    break
                else:
                    a = input('Jogador "X", digite um número de 0 a 8: ')
            if j[a] != ' ':
                a = input('Jogador "X" por favor, escolha outro campo: ')
            else:
                break
        j[a] = 'X'

You also have the option to set a function to check your move.

def check(ipt):
    if ipt.isnumeric() and int(ipt) <= 8:
        ipt = int(ipt)
        if j[ipt] != " ":
            return "Campo já preenchido"
        return True
    return "Erro de input"

while c <= 10:
    if c % 2 == 1:
        a = input('Jogador "X", onde pretende fazer a jogada? ')
        while check(a) is not True:
            if check(a) == "Erro de input":
                a = input('Jogador "X", digite um número de 0 a 8: ')
            elif check(a) == "Campo já preenchido":
                a = input('Jogador "X" por favor, escolha outro campo: ')
        a = int(a)
        j[a] = 'X'

Repl.it

  • Thank you very much, helped me very mainly in defining the function..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.