Home > Mobile >  Rock paper scissors not executing winning conditions properly
Rock paper scissors not executing winning conditions properly

Time:06-14

really quick problem I think its a dumb mistake I made. So anyway I never actually used multiple or and and in my codes, I started using it when dealing with data but I do a lot of errors, so I decided to run a rock paper scissors program using it and it the program exits without properly running the win condition. Here is my code, would appreciate any help!!

import random

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





def play_rps():
 resp='c'
 while resp.lower()!='n':
     user='x'
     while user not in ['r','s','p']:
         user = input("'r' for rock 'p' for paper 's' for scissors ")
         computer = random.choice(['r', 's', 'p'])
         print(f'You chose {user} and Computer chose: {computer} ')
 if computer == user:
     return 'Draw'
 elif isWin(user, computer):
     return 'You win!'
 return 'You lose!'

print(play_rps())

CodePudding user response:

You forgot to print it.

When you use a return, it gives or assigns the function a value instead of printing it

import random

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


def play_rps():
    user=''
    user=input("'r' for rock 'p' for paper 's' for scissors ")
    computer=random.choice(['r','s','p'])
    if computer==user:
        return 'Draw'
    elif isWin(user,computer):
        return 'You win!'
    return 'You lose!'


print(play_rps())

CodePudding user response:

You forgot to change the condition for outer while loop, therefor it goes in an infinite loop.

This should do the trick

import random

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

def play_rps():
 resp='c'
 while resp.lower()!='n':
     user='x'
     while user not in ['r','s','p']:
         user = input("'r' for rock 'p' for paper 's' for scissors ")
         computer = random.choice(['r', 's', 'p'])
         print(f'You chose {user} and Computer chose: {computer} ')
         recp = 'n'
 if computer == user:
     return 'Draw'
 elif isWin(user, computer):
     return 'You win!'
 return 'You lose!'

print(play_rps())
  • Related