Text Size
Text Size
Exit Full Screen
Python3 Editor
Show
Source Code
View Source Code in Full Screen
# import the random library so that the computer can choose randomly import random # list of all valid choices, each value is using all lowercase letters for easier input checking choices = ['rock', 'paper', 'scissors'] # ask the player to enter a choice and change it to all lowercase player = input("Enter a choice " + str(choices) +": ").lower() # randomly choose a value for the computer comp = random.choice(choices) # print the computer's choice print("Computer:", comp) # check if the player's choice is invalid (not in the list) if player not in choices: print("Invalid choice:", player) # check if the game ends in a tie elif player == comp: print("It's a tie!") # check the other 2 combinations for when player chooses 'rock' elif player == 'rock': # can nest if-statements inside of each other, just make sure spacing is correct if comp == 'paper': print("Paper covers Rock! You lose!") else: # comp == 'scissors' print("Rock crushes Scissors! You win!") # do the last two conditions on your own!
CS110_L4_RPS_Notes.py
( around 24 lines Python3 code )
Published By:
Mentor Sabrina
Created on
2020-11-02T14:19:30Z
Python3 Editor
- an
OYOclass
application,
own your own class today
.
Run
Result
Status:
Program is not running