Question

In python: The randrange(min, max) produces a random integer greater than or equal to min and...

In python:

The randrange(min, max) produces a random integer

greater than or equal to min and less than max.

So randrange(0,3) produces either 0, 1, or 2

Write a program that asks the user to input

paper, rock, scissors, or done

If they input something else, repeat  the question.

If they input 'done', exit the game loop.

If they input rock, paper, or scissors, have the

computer pick a random choice.

Write a function that takes two strings, each 'rock', 'paper', or 'scissors' and returns a value indicating whether the first string won, lost, or tied the contest.

(perhaps 1 for winning, 0 for a tie, -1 for a loss)

rules:

Rock smashes scissors. Scissors cut paper. Paper covers rock. If both players select the same choice, the round is a tie.

Use your function to determine the winner after each choice.

Keep track of how many times the player has won, lost, and tied.

Print whether the player or the computer won the round or if it is a tie.

When the game loop ends, print the number of wins, losses, and ties.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import random


# function that takes two strings, each 'rock', 'paper', or 'scissors'
# and returns a value indicating whether the first string won, lost, or
# tied the contest returns  1 for winning, 0 for a tie, -1 for a loss
def rock_paper_scissors(first, second):
    # return a integer as result based on rules
    # rules:
    # Rock smashes scissors.
    # Scissors cut paper.
    # Paper covers rock.
    # If both players select the same choice, the round is a tie.
    # check for first input
    if first == "rock":
        # check for second input
        if second == "paper":
            return -1
        elif second == "scissors":
            return 1
        else:
            return 0
    elif first == "paper":
        # check for second input
        if second == "scissors":
            return -1
        elif second == "rock":
            return 1
        else:
            return 0
    else:
        # check for second input
        if second == "rock":
            return -1
        elif second == "paper":
            return 1
        else:
            return 0


# computer get a choice randomly and returns a string with value
# 'rock', 'paper', or 'scissors'
def computer_choice():
    # get a random number from 0 to 2
    number = random.randrange(0, 3)
    # 0 indicates rock
    # 1 indicates paper
    # 2 indicates scissors
    # return the string of choice
    if number == 0:
        return "rock"
    elif number == 1:
        return "paper"
    else:
        return "scissors"


def main():
    # initialize variables to store result
    wins = 0
    losses = 0
    ties = 0
    # asks the user to input
    user_in = input("Enter your choice:\n  rock\n  paper\n  scissors\n  done\n")
    # continue the loop till user enter done
    while True:
        # check for user input
        # ignore case by converting user input into lower case
        if user_in.lower() == "done":
            # break the loop as user done playing
            break
        elif user_in.lower() == "rock" or user_in.lower() == "paper" or user_in.lower() == "scissors":
            # take a random choice from computer and see if user won
            comp_choice = computer_choice()
            result = rock_paper_scissors(user_in, comp_choice)
            # check the result and print it
            if result == 1:
                print("You Won!")
                wins += 1
            elif result == 0:
                print("Its a Tie!")
                ties += 1
            else:
                print("You Lost!")
                losses += 1
        # ask user again for new input
        user_in = input("Enter your choice:\n  rock\n  paper\n  scissors\n  done\n")
    # at end of game print result
    print("Thanks for playing!")
    print("Wins: " + str(wins))
    print("Losses: " + str(losses))
    print("Ties: " + str(ties))


if __name__ == '__main__':
    main()

let me know if you have any problem or doubts.

Add a comment
Know the answer?
Add Answer to:
In python: The randrange(min, max) produces a random integer greater than or equal to min and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • The game lets players know whether they have won, lost, or tied a particular game. Modify...

    The game lets players know whether they have won, lost, or tied a particular game. Modify the game so the players are told the number of games they have won, lost or tied since the start of game play. Implement this change by adding three variables named $wins, $lost, and $ties to the program's Main Script Logic section, assigning each an initial value of 0. Next, modify the analyze_results method by adding programming logic that increments the values of $wins,...

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the...

  • Create a python program to play rock paper scissors using a while loop and if statements....

    Create a python program to play rock paper scissors using a while loop and if statements. I have started but have gotten stuck in a continuous loop. Please look over my code and help me find where I went wrong. Here it is: import random #Choice weapons=['Rock' ,'Paper' ,'Scissors'] print('Rock, Paper, Scissors!') print('Rock, Paper, Scissors!') print('Shoot!') human=input('Choose Rock, Paper, Scissors, or Quit! ') print('')#Blank Line while human != 'Quit': human_choice=human computer=random.choice(weapons) print(computer) if human==computer: print("It's a tie!") elif human=='Rock': if...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • Use Dev C++ for program and include all of the following. Im lost. Make sure the...

    Use Dev C++ for program and include all of the following. Im lost. Make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”. Ask the user how many times he/she wants to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT