Tag: Selection

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

Python Selection – if:

diceToday we investigated the use of the if statement, this is a selection statement which allows Python to make choices.

We have updated our dice roll program from last time to include a for loop and multiple if statements. The program now displays a summary of the results of 1000 dice rolls.

For homework the class were asked to TRACS the program and we will discuss how efficient it is next time.

Using a message box to make a choice

It is possible to use a MsgBox to make a choice in your program. You first of all assign a integer variable to the MsgBox then you change the buttons attributes to vbYesNo. vbYesNo is a VB constant that represents a value, the use of the constant means that you do not need to memories the number, vbYes and vbNo are other examples of these VB constants. An IF statement is then used to make the selection.

N5