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!

Answered by Alfredo R. Python tutor

1175 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a function that checks whether a number is prime or not.


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10


Create a python code to sum the number from 1 to 10.


What can I do with Python?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy