Inverting variables (Python)

Asked

Viewed 46 times

1

I know you can do it in other ways, but I’ve done it limited to ifs. I wonder why I’m reversing the results in the last part of the code.

from random import randint
    
pedra, papel, tesoura = "1", "2", "3"
    
print("[1] Pedra\n[2] Papel\n[3] Tesoura")
p = int(input("Pense no que vai jogar: \n"))
d, e, c = "Você perdeu!", "Empate!", "Você ganhou!"
pe, pa, te = "Pedra", "Papel", "Tesoura"
    
if p == 1:
    print("Você jogou:", pa)
elif p == 2:
    print("Você jogou:", pe)
elif p == 3:
    print("Você jogou:", te)
    
pc = randint(1, 3)  
    
if pc == 1:
    print("PC jogou:", pa)
elif pc == 2:
    print("PC jogou:", pe)
elif pc == 3:
    print("PC jogou:", te)
    
if pc == p:
    print(e)
elif pc == 1 and p == 2:
    print(d)
elif pc == 2 and p == 1:
    print(c)
elif pc == 3 and p == 1:
    print(d)
elif pc == 1 and p == 3:
    print(c)
elif pc == 3 and p == 2:
    print(c)
elif pc == 2 and p == 3:
    print(d)
  • 2

    We speak Stone, Paper and Scissors... You put 1 = paper, 2 = stone, 3 = Scissors. Then you left your code confused. Change the values to 1 = stone, 2 = paper, 3 = scissors and redo the if. It will make it easier for you. The error is in the other ifs and not in the last two.

1 answer

1

elif pc == 1 and p == 2:
    print(c)
elif pc == 2 and p == 1:
    print(d)
elif pc == 3 and p == 1:
    print(c)
elif pc == 1 and p == 3:
    print(d)
elif pc == 3 and p == 2:
    print(d)
elif pc == 2 and p == 3:
    print(c)

The problem was that where the player won, he was imprinted with the varietal that showed him as a loser. Just replace, in the last if, where you have "c" by "d" and vice versa, as shown in the code above.

Browser other questions tagged

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