Prompt user to select either rock or scissors instruct


Assignment: Rock-Paper-Scissors

In this project, we'll build Rock-Paper-Scissors from Scatch in IDLE!

The program should do the following:

1. Prompt the user to select either Rock, Paper, or Scissors
2. Instruct the computer to randomly select either Rock, Paper, or Scissors
3. Compare the user's choice and the computer's choice
4. Determine a winner (the user or the computer)
5. Inform the user who the winner is

1. As in previous projects, it's helpful to let other developers know what your program does.

2. Begin by including a multi-line comment that describes what your program will do.

3.

4.

5. This game will also require Python code that isn't built-in or readily available to us. Since the program will select Rock, Paper, or Scissors, we need to make sure the computer randomly selects one option.

6. Use a function import to import randint from the random module.

7. On the next line, use a function import to importsleep from the time module.

8. Great! We've imported the code we'll need. We won't use it right now, but we will need it later, so let's move on.

9. In this game, we know there are a few things that are constant (things that will not change). For example, the options (Rock, Paper, or Scissors) will remain constant, so we can add those options and store them in a variable.

10. On the next line, create a list called options and store "R", "P", and "S" in the list (as strings). Abbreviating the options will come in handy later.

11. The user will always either win or lose, so the corresponding win/lose messages that we print to the user will also remain constant.

12. On the next line, create a variable and set it equal to"You lost!", or another message of your choice that indicates the user lost.

13. Note: Want to code like a professional? Variables that store constant information should be typed in snake case and should be entirely uppercase, as indicated in the Python style guide.

14. On the next line, create a variable and set it equal to a winning message, similar to what you did in Step 5.

15. Since the user must select an option and the computer must also select an option, we need a way to decide who the winner is.

16. Add a function called decide_winner. The function should take two parameters:user_choice and computer_choice.

17. Great! Let's start building the decide_winnerfunction.

18. First, use string formatting to print to the user's choice. Since the user's choice is already a parameter of the function, you can use the same parameter when using string formatting.

19. On the next line, print Computer selecting...and then have the program sleep for 1 second.

20. On the next line, use string formatting to print the computer's choice, similar to what you did in Step 8.

21. How will we compare the user's selection to the computer's selection?

22. Thankfully, we stored the options (Rock, Paper, and Scissors) in a list that will remain constant. Since items in a list all have an index, we can simplify how the comparison should take place: we will compare the index of the user's choice and the index of the computer's choice.

23. Now we have to figure out how will we determine the index of the user's choice. Lucky for us, lists have a built-in function called index().

24. Given an item that belongs to a list, the index()function will return the index of that item. Read more about how index() works here.

25. On the next line, create a variable calleduser_choice_index. Set the variable equal to the result of calling the index() function on theoptions list.

26. The index() function should take user_choiceas an argument.

27. On the next line, create a variable to store the index of the computer's choice. Set it equal to calling theindex() function on the options list. The function should take the computer's choice as an argument.

28. Perfect! You just completed the most challenging part of the project. Now it's time to code the rules that will determine the winner.

29. Start by adding an if statement that checks if the user's choice is equal to the computer's choice.

30. What happens when both players pick the same option? It's a tie!

31. Inside the if statement, print a message to the user informing them of the tie.

32. Now it's time to think of the scenarios in which players win or lose.

33. Wait a minute...there are many different scenarios, and they're going to take a long time to code. Part of being a professional programmer is figuring out fast and efficient ways of solving problems, like this one.

34. Let's approach this problem with a glass half full mentality: we'll print only the scenarios in which the user wins, otherwise the user will have lost.

35. What are the scenarios in which the user wins? User: Rock, Computer: Scissors User: Paper, Computer: Rock User: Scissors, Computer: Paper

36. Each option has a constant index in the optionslist, and we can use that to our advantage.

37. Add an elif statement that checks if the user selects "Rock" and the computer selects "Scissors." Inside the statement, print the win message. Remember, Rock has an index of 0 and Scissors has an index of 2.

38. Perfect! But that takes care of only one scenario where the user wins.

39. Add two more elif statements that print the win message when the user wins. You can use the scenarios from Step 16 to help you.

40. What if the user's choice has an index greater than 2? That's garbage! Add one more elif statement that checks for this condition.

41. Inside of the elif block, print a message to the user that indicates that an invalid option was selected. On the next line, use return to exit the block.

42. Finally, we've taken care of all of the cases where the user could win. But what if none of these conditions are met? Remember from Step 16 that the user would lose.

43. Add an else block and print the loss message inside of it.

44. Great! We have the function that will decide who the winner is between the user and the computer, but we haven't written a function that actually starts the game. Let's do that now.

45. Create a new function called play_RPS.

46. On the next line and within the function, print the name of the game to the user.

47. On the next line, we'll have to prompt the user for their selection. Store their selection in a variable called user_choice.

48. To make it simpler, we should ask them to input as few characters as possible for their selection. Prompt them with the message:Select R for Rock, P for Paper, or S for Scissors:

49. Then, on the next line, sleep the program for 1 second.

50. Convert the user's choice to uppercase. This will match the format used in the options list we created earlier.

51. The computer has to play too! Remember, the computer's choice has to be random, so we'll make use of randint to accomplish that.

52. On the next line, create a variable calledcomputer_choice. Set the variable equal to a random element of the options list using randintand the list indices.

53. Remember, this is how the randint function works.

54. Actually, in the last step, we hard coded the random possibilities of the options list, but what if there were more possible options in the list? It wouldn't be efficient to always have to open up the file just to change the indices in the one line of code you just wrote.

55. First, delete the line of code you wrote in the Step 24.

56. Create a variable called computer_choice. As in the last step, set it equal to a random element of the options list using randint. However, instead of using 0 and 2 in the randint function, use 0as the first integer, and use len(options)-1 as the second integer.

57. This will ensure that if we ever add more options to the game, we won't have to change this line of code. (Of course, there might be more rules.)

58. Great! The user has now submitted their choice and the computer has also made a random choice. It's time to determine a winner. Thankfully, we already wrote a function that can do that.

59. On the next line, call the decide_winner function. Pass in user_choice as the first argument andcomputer_choice as the second argument.

60. Our program won't run unless we call the correct function! On the next line, call the play_RPS()function. Make sure it's outside of any other function.

Request for Solution File

Ask an Expert for Answer!!
Python Programming: Prompt user to select either rock or scissors instruct
Reference No:- TGS02300419

Expected delivery within 24 Hours