2

I am creating a Rock, Paper, Scissors game for a class. as part of the game I need to have a weapon menu display to the screen for the user to select from. Then the computer will randomly select a weapon from a list. The problem I am facing (I believe) is that the list items range from [0,2] where my menu items list [1,3]. I have searched around for hours, but I don't understand the complex things I have been reading online so I'm not certain how to apply them.

# random integer
from random import randint

# list for weapon
WEAPON = ["Rock", "Paper", "Scissors"]

# one player mode
def onePlayer():
scoreP = 0
scoreC = 0
again = ""
player = False

print("---------------------------------------------")
print("\n\tPlayer VS Computer")

while player == False:
    print("Weapons:")
    print("1. Rock")
    print("2. Paper")
    print("3. Scissors")
    print("4. Quit")

    player = input("\nSelect your weapon: ")
    if player == "quit" or player == "q" or player == "4":
        player = True
        main()
    else:
        try:
            player = int(player)
            if player == 1:
                player = WEAPON[0]
            elif player == 2:
                player = WEAPON[1]
            elif player == 3:
                player = WEAPON[2]
        except:
            print("please enter a number 1 through 4\n")

    computer = WEAPON[randint(0,2)]

    if player == computer:
        print(player," vs ",computer)
        print("It's a tie!\n")
        print("Player:",scoreP,"\nComputer:",scoreC)
        print("")
    elif player == 1:
        # computer == paper
        if computer == 1:
            print(player," vs ",computer)
            print("Paper covers rock! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Rock smashes scissors. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 2:
        if computer == 2:
            print(player," vs ",computer)
            print("Scissors cut paper! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Paper covers rock. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 3:
        if computer == 0:
            print(player," vs ",computer)
            print("Rock smashes scissors! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Scissors cut paper. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    #else:
    #    print("Please select a valid play option\n")
    player = False       

Please don't mind the print statements inside the if/else statements. I realize these will need to be changed. My main issue is the logic of comparing user input to the computer's random list selection.

1
  • What's wrong with the code? Could you make it more concise? (What's the question?) Commented Jul 28, 2016 at 14:42

4 Answers 4

4

You need to be careful with the contents of your variables:

# this is storing a string     
computer = WEAPON[randint(0,2)]

# this expects an integer
elif player == 1:
        # computer == paper
        if computer == 1:

That would be the root of some of the problems that you are seeing.

Also, in general, when coding try to use meaningful variable names and avoid reusing them for more than one purpose: In this case, two new variables like player_weapon and computer_weapon (instead of reusing player and computer) would have probably prevented your bug. Don't be lazy when declaring variables! ;)

Sign up to request clarification or add additional context in comments.

Comments

1

In the if statements, it seems like you are comparing the variable computer, which is a string, to an integer. You assign computer = WEAPON[randint(0,2)], so computer is one of the following: ["Rock", "Paper", "Scissors"]. However, in your if statements, you are saying: if computer == 1: to compare it with the person (your person variable is the same way; you assign a string to it before you compare it to integers).

You just have to make sure you are comparing apples to apples

Comments

1

Compare to the strings, not to the numbers, like this

    if player == computer:
        print(player," vs ",computer)
        print("It's a tie!\n")
        print("Player:",scoreP,"\nComputer:",scoreC)
        print("")
    elif player == 'Rock':
        # computer == paper
        if computer == 'Paper':
            print(player," vs ",computer)
            print("Paper covers rock! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Rock smashes scissors. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 'Paper':
        if computer == 'Scissors':
            print(player," vs ",computer)
            print("Scissors cut paper! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Paper covers rock. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 'Scissors':
        if computer == 'Rock':
            print(player," vs ",computer)
            print("Rock smashes scissors! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Scissors cut paper. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    #else:
    #    print("Please select a valid play option\n")
    player = False

Comments

1

I have condensed a majority of your code by implementing a small dict_map. It could be condensed further but why bother.

# random integer
from random import randint

# list for weapon
WEAPON = ["Rock", "Paper", "Scissors"]
MAPP = {"Rock":{"Win":'Scissors', "Loss":"Paper", "Adj":"Smashes"},
        "Paper":{"Win":"Rock", "Loss":"Scissors", "Adj":"Covers"},
        "Scissors":{"Win":"Paper", "Loss":"Rock", "Adj":'Cuts'}}

def have_won(player, computer):
    #determines if the players choice has beaten the computers
    if MAPP[player]["Win"] == computer:
        adj = MAPP[player]['Adj']
        return True, ' '.join([player, adj, computer])
    else:
        adj = MAPP[computer]['Adj']
        return False, ' '.join([computer, adj, player])

# one player mode
def onePlayer():
    scoreP = 0
    scoreC = 0
    again = ""
    player = False

    print("---------------------------------------------")
    print("\n\tPlayer VS Computer")

    while player == False:
        print("Weapons:")
        print("1. Rock")
        print("2. Paper")
        print("3. Scissors")
        print("4. Quit")

        player = input("\nSelect your weapon: ")
        if player == "quit" or player == "q" or player == "4":
            player = True
        else:
            try:
                player = int(player)
                if player == 1:
                    player = WEAPON[0]
                elif player == 2:
                    player = WEAPON[1]
                elif player == 3:
                    player = WEAPON[2]
            except:
                print("please enter a number 1 through 4\n")

        computer = WEAPON[randint(0,2)]
        print player, computer
        outcome = have_won(player, computer)
        if player == computer:
            print(player," vs ",computer)
            print("It's a tie!\n")
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")

        elif outcome[0] == True:
            print(outcome[1]+"! You Win!!")
            scoreP += 1
        elif outcome[0] == False:
            print(outcome[1]+"! You Lose!!")
            scoreC += 1
        #else:
        #    print("Please select a valid play option\n")
        print("Player:",scoreP,"\nComputer:",scoreC)
        player = False
onePlayer()

2 Comments

This is more advanced then what I've learned in my class, but it's basically what I was thinking in my head to accomplish. Thank you very much! At the moment I'm a little confused because I do need to provide a way of validating the user input, but I'm not certain what area of the code is processing the input and how.
Currently there is little to no validation. This could simply be added in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.