Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

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.

Python, Programming

  • Category:- Python
  • Reference No.:- M92313590

Have any Question?


Related Questions in Python

The second task in this assignment is to create a python

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.(PYTHON 3) Problem Task In this problem, your input w ...

Assignment1 utilising python 3 build the following

Assignment 1. Utilising Python 3 Build the following regression models: - Decision Tree - Gradient Boosted Tree - Linear regression 2. Select a dataset (other than the example dataset given in section 3) and apply the De ...

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

Project reconnaissance and attack on ics

Project: Reconnaissance and Attack on ICS NetworksEnvironment Setup The second mini project will be based on Industrial Network Protocols, specifically the Modbus protocol. Please follow the instructions carefully to set ...

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

Show times in tmus and seconds1 an associate grasps an oven

Show times in TMUs and seconds. 1. An associate grasps an oven door within reach and pulls it open 18 inches with the left hand (he does not relinquish control of the door). With a pan in the right hand, he carefully pos ...

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Question write a python program with a graphical user

Question: Write a python program with a graphical user interface that will allow a user to create a custom pizza which they wish to order. At minimum, the user should be able to choose the size of the pizza, the type of ...

Simple python traffic lightswrite a program that simulates

Simple Python (Traffic lights) Write a program that simulates a traffic light. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and onl ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As