Type error: not all strings converted during formatting

0

I'm trying to make a couple or odd game. The problem is that every time the sum of my play with that of the computer gives 'par' this error message appears. I've tried other types of formatting and the same error always appears. Also, this line is pretty much the same as the one above, but the above line does not give error, but for some reason, this line gives. Follow the code

from random import randint

pc = randint(0,10)

print(' =-'*20)
print('VAMOS JOGAR PAR OU ÍMPAR')
print(' =-'*20)

vitoria = 0


while True:
    vc = int(input('Diga um valor: '))
    j = str(input('Par ou Ímpar? [P/I]: '))
    while vc < 0 or vc > 10:
        vc = int(input('Jogada inválida. Digite um número de 1 a 10: '))
        j = str(input('Par ou Ímpar? [P/I]: '))
    soma = vc + pc
    if soma % 2 == 0:
        print(f'Você jogou {vc} e o computador jogou {pc}.Total de {soma} deu PAR')
        soma = 'p'
    if soma %2  != 0:
        print(f'Você jogou {vc} e o computador jogou {pc}.Total de {soma} deu IMPAR')
        soma = 'i'
    if j != soma:
        break
    vitoria = vitoria + 1
print('VOCÊ PERDEU!')
print('=-'*20)
print(f'GAME OVER! Você venceu {vitoria} vezes')

Error Print

  

Edit: I managed to get around the problem by putting an 'else' in place of another 'if' condition. But I would still like to know why this happened.

    
asked by anonymous 18.12.2017 / 01:22

1 answer

0

Your mistake is to use the same object, soma , to store both the result of the sum of the numbers and the letter that identifies the result. In addition to being a bad approach, as it does not leave the code readable, it makes room for mistakes as it has.

When the result of the sum of the values is even, the condition of the first if , of line 19, is satisfied and its code block is executed. On line 21, within if , you change the value from soma to 'p ', to indicate that the result was even, but on the line immediately below you try to compute the rest of the soma by 2; At this point, soma is no longer an integer value, but a string 'p' and, in Python, the remainder operator is for string formatting, which explains the error message. This incoherence of Python has already been corrected, but not omitted, by implementing the str.format method as well as described in that question:

Character formatting

For your problem, the simplest solution is to change the name of the object to one of the points in the program; for example, instead of soma = 'p' , do something like resultado = 'p' and at the end check j != resultado .

An alternative to your code would be:

from random import randint

print('=-'*20)
print('VAMOS JOGAR PAR OU ÍMPAR')
print('=-'*20)

vitorias = 0

while True:
    while True:
        voce = int(input("Que número entre 0 e 10 você jogará? "))
        if not 0 < voce < 10:
            print("Valor inválido, diga um número entre 0 e 10")
            continue
        break
    while True:
        resposta = input("[P]ar ou [I]mpar?").upper()
        if resposta not in ('P', 'I'):
            print("Entre com P para par ou I para ímpar")
            continue
        break
    computador = randint(0, 10)
    soma = voce + computador
    resultado = 'P' if soma % 2 == 0 else 'I'
    print(f"Você: {voce} / Computador: {computador} / Soma: {soma}")
    if resultado == resposta:
        vitorias += 1
    else:
        break
print('=-'*20)
print(f'GAME OVER! Você venceu {vitorias} vezes')

See working at Repl.it

Considerations

To explain the differences between the codes, let's see:

  • Reading the value that the player wants is done within an infinite loop; this is because we do not know how many times it will enter an invalid value and prevents you from knowing the value in advance - that is, you only read in one place of the code only, you do not need to double-call input() for the same thing (redundancy) ;

  • )
  • li>
  • Reading the user's even or odd response is also validated, ensuring that it indicates a response the program can handle - which its original code does not. The logic is exactly the same to read the integer value, but checking if the player's response was not 0 < voce < 10 or voce < 0 or voce > 10 . I also used the 'P' method to ensure that the player input is always in the uppercase, which allows you to enter with 'I' or str.upper() too;

  • As I mentioned earlier, I used a different object to store the game result, and I kept the 'p' object to store the sum of the values. The object 'i' will receive soma when the sum is even and resultado when odd, as in your code, but in a simpler way;

  • Another important detail was that I put the 'P' function into the infinite loop, so that the response of the computer is different for each round. The way you did, the value will be the same in all of them;

  • 18.12.2017 / 01:48