Compare the users choice and the computers choice

Assignment Help Python Programming
Reference no: EM131504861

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.

Reference no: EM131504861

Questions Cloud

Which is better for american investor week or strong dollar : Which is better for an american investor a week or strong dollar ? Why international investing is more risker than domestics ?
What is the value of the rms current in the coil : What is the value of the rms current in the coil if the coil has the resistance of 3.3 O?
Describe a decision-making process for employees : Describe a decision-making process for employees with two different strengths. Explain how a manager could develop employees with different strengths.
Find the magnitude of an electric field : Find the magnitude of an electric field located 2.00 cm from a source charge of 7.0 nC. (1nC = 1 x 10-9 C).
Compare the users choice and the computers choice : Prompt user to select either Rock or Scissors. Instruct computer to randomly select either Paper or Scissors. Compare user's choice and the computer's choice
Create a new product that would appeal to your market : Choose the target market for your product, and describe your target market in detail.Create a new product that would appeal to your market.
High-level funding plan develop high-level funding schedule : High-Level Funding Plan Develop a high-level funding schedule for a new venture business concept that can be used to discuss cash requirements for first year.
Change the osmotic balance : Too much water is a problem for the human body because it can change the osmotic balance?
Examine the proposed business ethical problem : Examine the proposed business ethical problem that the Brazilian Federal Data Processing Service is presently experiencing.

Reviews

Write a Review

Python Programming Questions & Answers

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd