Ex8 - The if-elif-else Chain:
Objective: Write a conditional test using an if-elif-else chain to create the game rock, paper, scissors.
Description: We want our game to have the computer play against the user. Name the file: Ex8-if-elif-else-chain.py.
- Create the computer’s choice as scissors. To do so, create the variable,
computerChoiceand assign it the value of scissors. - Allow the user to enter either rock, paper, or scissors by using the input function with the message “Do you want rock, paper, or scissors?”. This message (along with the input function) should be assigned to the variable,
userChoice. - First, handle the tie outcome.
- a. Start with an if statement for if computerChoice == userChoice. Then print TIE. At this point, check your work! Run your code, type scissors into the terminal, and confirm that TIE gets printed.
- Next, handle the win outcomes for the user.
- a. Create an elif statement that checks if userChoice == “rock” and computerChoice == “scissors”. Then print WIN.
- b. Handle the next way the user can win. If the user chooses paper, and the computer chooses rock. In other words, if userChoice is equal to paper, and computerChoice is equal to rock, we’ll print WIN again.
- c. Handle the last way the user can win. Copy the elif code block again and replace userChoice with scissors and computerChoice with paper.
- Finally, handle the lose outcome for the user.
- a. Create an else block and print “You lose, computer wins”
- Solution Output Examples:
Example output of a TIE outcome:
1Do you want rock, paper, or scissors?
2scissors
3TIEExample output of a WIN outcome:
1Do you want rock, paper, or scissors?
2rock
3WINExample output of a LOSE outcome:
1Do you want rock, paper, or scissors?
2paper
3You lose, computer wins