Tag: ELIF

Higher – Rock Paper Scissors – Solution

So when we implemented the code we got something like this

#Mr Stratton

import random

#set up variables
random.seed
object=["Rock","Paper","Scissors"]
playerChoice=""
computerChoice=""
winner=""

#Get players choice
playerChoice=object[int(input("Rock - 1\nPaper - 2\nScissors - 3\n"))-1]

#Get computer choice
computerChoice=object[random.randint(0,2)]

#get winner
if playerChoice=="Rock":
    if computerChoice=="Paper":
        winner="Computer"
    if computerChoice=="Scissors":
        winner="Player"
    if computerChoice==playerChoice:
        winner="Draw"
        
if playerChoice=="Paper":
    if computerChoice=="Scissors":
        winner="Computer"
    if computerChoice=="Rock":
        winner="Player"
    if computerChoice==playerChoice:
        winner="Draw"
        
if playerChoice=="Scissors":
    if computerChoice=="Rock":
        winner="Computer"
    if computerChoice=="Paper":
        winner="Player"
    if computerChoice==playerChoice:
        winner="Draw"
        

    
#display winner
print("\n"*10)
print("Player threw",playerChoice)
print("Computer threw",computerChoice)

if winner=="Player":
    print("Player is the winner")
    
if winner=="Computer":
    print("Computer is the winner")
    
if winner=="Draw":
    print("Its a draw")


However, although it does follow the design from yesterday it isn’t very effcient.
Can you see a way to make it more effient?

#Mr Stratton

import random

#set up variables
object=["Rock","Paper","Scissors"]
playerChoice=""
computerChoice=""
winner="Draw"

#Get players choice
playerChoice=object[int(input("Rock - 1\nPaper - 2\nScissors - 3\n"))-1]

#Get computer choice
computerChoice=object[random.randint(0,2)]

#get winner
if playerChoice=="Rock":
    if computerChoice=="Paper":
        winner="Computer"
    elif computerChoice=="Scissors":
        winner="Player"

        
elif playerChoice=="Paper":
    if computerChoice=="Scissors":
        winner="Computer"
    elif computerChoice=="Rock":
        winner="Player"

        
elif playerChoice=="Scissors":
    if computerChoice=="Rock":
        winner="Computer"
    elif computerChoice=="Paper":
        winner="Player"

        
else:
    print("Error in player choice")
    
#display winner
print("\n"*10)
print("Player threw",playerChoice)
print("Computer threw",computerChoice)
if winner=="Player":
    print("Player is the winner")
elif winner=="Computer":
    print("Computer is the winner")
else:
    print("Its a draw")

Read more