How compare if a variable (x) is greater than another variable (y) by a certain value. For example, if x > y by 2, print("Hooray!")

63 Views Asked by At

So I'm trying to make a rock, paper, scissors game and I'm trying to make it so that if the user (or computer) either gets 3 points OR the user's (or computer's) score is greater by 2 (like a best 2 out of 3 game) then the user (or computer) wins. But I can't seem to get it to work. I just started learning Python and programming in general so I'd appreciate an explanation that's as simple as possible.

Here is the code that I tried:

def rpc_game_normal ():
    rpc = ['r', 'p', 's']
    computer_wins = 0
    wins = 0
    


    while True: 
        print(f"Me: {computer_wins}  | You: {wins} ")
        user = input('Your move human: ').lower()
        computer = random.choice(rpc)

        if user == computer:
            wins += 0
            computer_wins += 0
        elif pattern(user, computer):
            computer_wins += 1
        else:
            wins += 1
        
        score(wins, computer_wins)
        print(f"I chose {computer} and you chose {user}.")
        
        
def pattern(user, computer):
    if (user == 'r' and computer == 's') or (user == 'p' and computer == 'r') or \
        (user == 's' and computer == 'p'):
        return True
def score(user, computer):
    if computer + 2 > user:
        return True
    elif user + 2 > computer:
        return True
    elif user == 3:
        print("You win!")
        return True
    else: 
        print("I win!")
        return True

Me: 0 | You: 0 Your move human: r I chose s and you chose r. Me: 1 | You: 0 Your move human: s I chose s and you chose s.

This is the outcome of what I get. I just get this same loop repeated over and over again even if the criteria is met.

1

There are 1 best solutions below

0
Ruben Sutton On

You could simply use


player = 3
computer = 1
if computer - player == 2:
     print("computer wins")
elif player - computer == 2:
     print("player wins")

This should solve it just by working out if the difference is 2.