I want my variable to always be whole

0

People need to get out of a drama in which I can not finish my logic without getting stuck in condition in condition, the code is part of a game of the old one that I am perfecting, my condition in the code below says that the value of the input user has to be NUMERIC and if it is numeric it has to be

asked by anonymous 05.08.2017 / 02:09

1 answer

0

You need to make two whiles inside one another. 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 entry and then if the field of play 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

    
07.08.2017 / 20:05