Giter Club home page Giter Club logo

legendisawesomes's Projects

icarusmod icon icarusmod

The Superior Freedom Networks expansion plugin for the TotalFreedomMod

rock-paper-and-scissors- icon rock-paper-and-scissors-

In this project, we'll build Rock-Paper-Scissors! The program should do the following: Prompt the user to select either Rock, Paper, or Scissors Instruct the computer to randomly select either Rock, Paper, or Scissors Compare the user's choice and the computer's choice Determine a winner (the user or the computer) Inform the user who the winner is Let's begin! Tasks 29/29Complete Mark the tasks as complete by checking them off Rock, Paper, Scissors 1. As in previous projects, it's helpful to let other developers know what your program does. Begin by including a multi-line comment that starts on line 1 that describes what your program will do. You can use the instructions above to help you write the comment. Stuck? Get a hint 2. 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. Use a function import to import randint from the random module. Stuck? Get a hint 3. On the next line, use a function import to import sleep from the time module. Stuck? Get a hint 4. 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. 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. 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. Stuck? Get a hint 5. The user will always either win or lose, so the corresponding win/lose messages that we print to the user will also remain constant. 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. 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. Stuck? Get a hint 6. On the next line, create a variable and set it equal to a winning message, similar to what you did in Step 5. Stuck? Get a hint 7. 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. Add a function called decide_winner. The function should take two parameters: user_choice and computer_choice. Stuck? Get a hint 8. Great! Let's start building the decide_winner function. 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. On the next line, print Computer selecting... and then have the program sleep for 1 second. Stuck? Get a hint 9. On the next line, use string formatting to print the computer's choice, similar to what you did in Step 8. Stuck? Get a hint 10. How will we compare the user's selection to the computer's selection? 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. 11. 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(). 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. On the next line, create a variable called user_choice_index. Set the variable equal to the result of calling the index() function on the options list. The index() function should take user_choice as an argument. Stuck? Get a hint 12. On the next line, create a variable to store the index of the computer's choice. Set it equal to calling the index() function on the options list. The function should take the computer's choice as an argument. Stuck? Get a hint 13. Perfect! You just completed the most challenging part of the project. Now it's time to code the rules that will determine the winner. Start by adding an if statement that checks if the user's choice is equal to the computer's choice. Stuck? Get a hint 14. What happens when both players pick the same option? It's a tie! Inside the if statement, print a message to the user informing them of the tie. Stuck? Get a hint 15. Now it's time to think of the scenarios in which players win or lose. 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. 16. 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. What are the scenarios in which the user wins? User: Rock, Computer: Scissors User: Paper, Computer: Rock User: Scissors, Computer: Paper Each option has a constant index in the options list, and we can use that to our advantage. Add an elif statement that checks if the user selects "Rock" and the computer selects "Scissors." Inside the statement, print the win message. Stuck? Get a hint 17. Perfect! But that takes care of only one scenario where the user wins. 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. Stuck? Get a hint 18. 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. 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. Stuck? Get a hint 19. 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. Add an else block and print the loss message inside of it. Stuck? Get a hint 20. 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. Create a new function called play_RPS. Stuck? Get a hint 21. On the next line and within the function, print the name of the game to the user. Stuck? Get a hint 22. On the next line, we'll have to prompt the user for their selection. Store their selection in a variable called user_choice. 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: Then, on the next line, sleep the program for 1 second. Stuck? Get a hint 23. Convert the user's choice to uppercase. This will match the format used in the options list we created earlier. Stuck? Get a hint 24. 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. On the next line, create a variable called computer_choice. Set the variable equal to a random element of the options list using randint and the list indices. Remember, this is how the randint function works. Stuck? Get a hint 25. 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. 26. First, delete the line of code you wrote in the Step 24. 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 0 as the first integer, and use len(options)-1 as the second integer. 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.) Stuck? Get a hint 27. 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. On the next line, call the decide_winner function. Pass in user_choice as the first argument and computer_choice as the second argument. Stuck? Get a hint 28. 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. Stuck? Get a hint 29. You worked really hard to create this game. Now it's time to sit back and be amazed at how far you've come! First, click Save. Then, in the terminal, type the following command and press "Enter" on your keyboard: python RPS.py Did you win? If not, try until you do! Feel free to add more functionality to your game. Happy coding! Report a Bug If you see a bug or any other issue with this page, please report it here. Tasks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.