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

1600 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

What is the outcome of the following code? print(2*[3,4,5])


Explain how python programs are structured and give an example of how methods are initiated


What is the difference between a list and an array?


Write a Python script to take a product name as input and then automatically google search reviews for it and open the top 3 search results in different tabs


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–2025

Terms & Conditions|Privacy Policy
Cookie Preferences