Home > Software design >  Trying to code a rock , paper, scissors game in Python
Trying to code a rock , paper, scissors game in Python

Time:05-01

When the input is 'r' it works perfectly but when the input is 'p' or 'r' it just adds up these two letters to the word 'scissors´ from the input phrase

import random
def play():
   user = input("'r' for rock, 'p' for paper, 's' for scissors")
   computer = random.choice(['r','p','s'])

    if user == computer:
        return 'It`s a tie!'

    # r > s, s > p, p > r
    if is_win(user, computer):
        return 'You won!'

    return 'You lost'

def is_win(player, opponent):
    #return true if player wins
    #r>s, s>p, p>r
    if(player == 'r' and opponnent == 's') or (player == 's' and opponnent == 'p') or \
        (player == 'p' and opponnent == 'r'):
        return True

CodePudding user response:

You need to have a return False at the end of your is_win. You also have a typo (opponnent instead of opponent) in your is_win function.

Try the below:

def play():
    user = input("'r' for rock, 'p' for paper, 's' for scissors\n")
    computer = random.choice(['r','p','s'])
    
    print(f"You played {user} and the computer played {computer}")
    if user == computer:
        return 'It`s a tie!'
    if is_win(user, computer):
            return 'You won!'
    return 'You lost'

def is_win(player, opponent):
    if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r'):
        return True
    return False

CodePudding user response:

i might be wrong but on the is_win function u wrote "opponent" as "opponnent" def is_win(player, opponent): if(player == 'r' and opponnent == 's') or (player == 's' and opponnent == 'p') or
(player == 'p' and opponnent == 'r'):

  • Related