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;