Create an rock, paper, scissors game. The user should input one option, and the computer should play randomly.

Possible solution: Firstly, two variable need to be created. The first variable is the one associated to the user's choice, while the second one is the one for the computer. The variable associated to the user, is created using the input statement, which will allow the user to write in the console. The variable associated to the computer is created by randint(0,3). This will create a random number between 0 and 2 (included). Now let's say 0 is paper, 1 is rock, and 2 is scissors. For each possible play of the computer there are 3 possible plays for the user. This is, if the computer hits a 0 (paper) there are three possibilities: the user can choose rock, paper or scissors. In this sense there are a total of 3^2=9 possibilities. Each possibility is taken into account as an if statement. A final variable with the answer is created before the ifs. This variable is started as empty. Then for each possible selection of the computer, the selection of the user is checked and the answer variable is changed accordingly to the rules. Let me show an example. if(computer==0): if(user=='paper'): answer='There is a tie' if(user='scissors'): answer='The user wins' if(user='rock'): answer='The computer wins'Now it's your turn to make the other 6 possibilities. Finally remember to print the answer statement in the console. Also you can modify the game to play rock, paper, scissors, lizard, Spock. Remember this solution is not unique, try to make your own!

AR
Answered by Alfredo R. Python tutor

1685 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Ask the user for a number and output the smallest divisor, bigger than one, for the inputted number. Output "Prime" if the number is a prime number.


Generate an array of integers from 0 to 99 and split it into two smaller arrays. Each smaller array will contain half of the original.


What's the difference between a local and a global variable?


What is the difference between using range() and a list of values?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences