-2

The following is a part of code RockPaperScissors, I can't figure out how Python, interpret

if answer in ("y", "yes", "Y", "Yes", "Of course"):
    return answer

since this return as a True.

and this other part of code below returns an Integer

player = int(player)
if player in (1, 2, 3):
    return player

How is it possible that it returns True for one part and an Integer in otherpart since it is the same structure?

based on the code below:

def start():
    print "Let's play a game of Rock, Paper, Scissors"
    while game():
        pass
    scores()

def game():
    player = move()
    computer = random.randint(1, 3)
    result(player, computer)
    return play_again()

def move():
    while True:
        print
        player = raw_input("Rock = 1\nPaper = 2\nScissors = 3\nMake a move : ")
        try:
            player = int(player)
            if player in (1, 2, 3):
                return player
        except ValueError:
            print "Oops ! I didn't understand that, Please enter 1, 2 or 3."

def result(player, computer):
    print "1..."
    time.sleep(1)
    print "2..."
    time.sleep(1)
    print "3 !"
    time.sleep(0.5)
    print "Computer threw {0}!".format(names[computer])
    global player_score, computer_score
    if player == computer:
        print "Tie game"
    else:
        if rules[player] == computer:
            print "victory getting closer !"
            player_score += 1

def play_again():
    answer = raw_input("Would you like to play again ?")
    if answer in ("y", "yes", "Y", "Yes", "Of course"):
        return True
    else:
        print "ok that's enough for today !"

def scores():
    global player_score, computer_score
    print "HIGH SCORES"
    print "Player : " + player_score
    print "Computer :" + computer_score
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
dcas
  • 111
  • 1
  • 10
  • 2
    Why is that puzzling? The `answer in (...)` test is only true if the value in `answer` is also in that tuple. – Martijn Pieters Jul 09 '15 at 15:31
  • i edited back my question, hope you understand my concern – dcas Jul 09 '15 at 15:52
  • Neither code returns `True`. It returns the value of `answer` in one case, the value of `player` in the other case. `play_again()` returns `None` if the `if` statement was never true; a non-empty string is considered true, and `None` is considered false. So `play_again()` either returns one of those strings (`'y'`, `'Y'`, etc.) or it returns `None`, and those two objects have different [truth values](https://docs.python.org/2/library/stdtypes.html#truth-value-testing). – Martijn Pieters Jul 09 '15 at 15:55

4 Answers4

4

If answer is an element of ("y", "yes", "Y", "Yes", "Of course"), then that evaluates to True.

cdonts
  • 9,304
  • 4
  • 46
  • 72
3

It appears you think that return answer returns True. It does not.

It returns the value of answer, which will be a non-empty string equal to one of the strings in the tuple. So the function play_again() returns 'Y' if you typed that in, because 'Y' is in the tuple.

When answer has a value that is not in the list, the if suite is not executed, so the else: branch prints a message and the function ends without a return statement. Python then returns None instead.

So play_again() either returns a non-empty string, one that is a member of the tuple ("y", "yes", "Y", "Yes", "Of course"), or it returns None. game() returns that value unchanged, back to the while loop in start():

while game():
    pass

The while statement tests the truth value of the return value. A non-empty string is considered true (so the loop continues), and None is considered false (the loop ends).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Based on user input (which could be yes or no) assigned to variable answer Python checks to see this variable appears in ("y", "yes", "Y", "Yes", "Of course").

It's a little syntactic sugar.

You could also do:

for x in ("y", "yes", "Y", "Yes", "Of course"):
    if answer == x:
        return answer

More info on the python operands (e.g. is, is not and not in) in the docs.

Ewan
  • 14,592
  • 6
  • 48
  • 62
0

answer in ("y", "yes", "Y", "Yes", "Of course") evaluates to True if answer is equal to any of ("y", "yes", "Y", "Yes", "Of course")'s elements.

raymelfrancisco
  • 828
  • 2
  • 11
  • 21