0

i have been trying to make a python rock paper scissors game with my limited python knowledge but good understanding of computer/ programming logic. here is my code:

def Main():
from random import randint

global computerChoice
global userChoice
print "Ok, let's play 3 rounds."

for i in range(3):
    userChoice = raw_input("Rock, paper, or scissors? ")

    computerChoice = randint(1,3)

    if userChoice == "Rock" or "rock":
        userChoice = 1
        choiceCompare()

    elif userChoice == "Paper" or "paper":
        userChoice = 2
        choiceCompare()

    else:
        userChoice = 3
        choiceCompare()

def choiceCompare():
global userChoice
global computerChoice

if userChoice == computerChoice:
    print "I chose the same thing! It's a draw!"

elif userChoice != computerChoice:

    if userChoice == 1:

        if computerChoice == 2:
            print "Yes! I chose paper, you lost!"

        elif computerChoice == 3:
            print "Dang. I chose scissors, you win."

    elif userChoice == 2:

        if computerChoice == 1:

            print "Man. I chose rock, you win."

        elif computerChoice == 3:

            print "I chose scissors, you loose!"

    else:
        if computerChoice == 1:
            print "Ha! I chose rock, you loose."

        elif computerChoice == 2:
            print "Aww, man! I chose paper, you win."


if __name__ == "__main__":
Main()

when i run it, i get completely wrong outputs like this:

Ok, let's play 3 rounds.
Rock, paper, or scissors? paper
I chose the same thing! It's a draw!
Rock, paper, or scissors? paper
I chose the same thing! It's a draw!
Rock, paper, or scissors? paper
Dang. I chose scissors, you win.

or this:

Ok, let's play 3 rounds.
Rock, paper, or scissors? scissors
Yes! I chose paper, you lost!
Rock, paper, or scissors? scissors
Yes! I chose paper, you lost!
Rock, paper, or scissors? scissors
Dang. I chose scissors, you win.

however, rock seems to be working fine:

Ok, let's play 3 rounds.
Rock, paper, or scissors? rock
Dang. I chose scissors, you win.
Rock, paper, or scissors? rock
I chose the same thing! It's a draw!
Rock, paper, or scissors? rock
Yes! I chose paper, you lost!

can anyone tell me what they think is wrong? thanks in advance, Liam.

P.S. i think there is something wrong with my choiceCompare function in the nested if statements. I'm not quite sure what though. also, ignore the automatic coloring in the bits where i copied the text from the program running, as they are not actually python code but text from the program.

  • 3
    you haven't got proper indentation in def choiceCompare, your also declaring the two global variables AGAIN which means they will not have the values you want – fr1tz Apr 23 '14 at 00:41
  • I assume that the indentation issue is from pasting into Stack Overflow -- be careful with that in the future. Also, the global declaration does not impact the operation of the program -- though it is more than recommended against. – M. K. Hunter Apr 23 '14 at 00:52
  • @fr1tz i'm pretty sure you have to declare the global variables in each function you use them in so that the function recognizes them. i had the problem of the second function saying that it couldn't find the global variable "userChoice" or the other, and so i put in the global thing and it fixed it. – theweasels1 Apr 23 '14 at 01:07

2 Answers2

2

Your problem(s) start in these lines:

userChoice == "Rock" or "rock"

Either use userChoice.lower(), or use userChoice in ("Rock", "rock"). That'll help initially, and that's just at first glance.

To expand. The issue here is that you're comparing userChoice == "Rock", which is either True or False, then you're saying or "rock" which evaluates to True on its own (any non-empty string is True). See also: this answer on string truth values.

Community
  • 1
  • 1
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • That would be a nice syntax to have for readability's sake though. – Charles Clayton Apr 23 '14 at 00:58
  • thanks, i used the "userChoice in ("Rock", "rock")" and it solved the problem, but what is that doing vs the other thing you said, and just saying: "if userChoice == "rock" or userChoice == "Rock"' P.S. dont really know what "in" does, so that would also be good to know – theweasels1 Apr 23 '14 at 01:01
  • @theweasels1 - the `in` operator uses a class or object's `__contains__` method behind the scenes to test for membership inside of an iterable or other container. It's also the best way to determine if one string is a substring of another. The suggestion to use `.lower()` would allow you to accept any variation of rOck, RocK, ROCK, etc. It's coercing the entire user-supplied value to lower case before comparing. – g.d.d.c Apr 23 '14 at 01:15
0
userChoice == "Rock" or "rock"

This line will always evaluate to true because it is like (userChoice == "Rock") or "rock" Try this instead: userChoice == "Rock" or userChoice == "rock"

See this link for documentation of boolean operators: https://docs.python.org/3.1/library/stdtypes.html

M. K. Hunter
  • 1,778
  • 3
  • 21
  • 27