0

This is a small experiment I am trying in learning Python. Its meant to be a text based slot machine, where 3 slots are 'rolled' and 1 of the 4 possible choices is chosen for each If the 2 numbers match, 5 points are given. If all 3 match, 10 are given. After running this function the game prints their current score asks if the user wants to play again. At this portion of the code:

def ask():
    play_again = raw_input ("Do you want to play again? (Yes or No):")
    if play_again == "yes" or "Yes":
    run_machine()
    elif play_again == "No" or "no":
        print "Bye!"
        sys.exit(1)
    else:
        print "I don't know what that means"
        ask()

Where run_machine() is the main function. However regardless of the input, the program repeats. I am new to python so this might be something simple but I wanted to see what so far might be the problem. If it isn't something in this block then here is the full program:

import random
slot_1 = [1,2,3,4]
slot_2 = [1,2,3,4]
slot_3 = [1,2,3,4]
points = 0
def role(slot):
   return (random.choice(slot))
def run_machine():
    print "ROLLING!"
    first = role(slot_1)
    second = role(slot_2)
    third = role(slot_3)
    global points
    if (first == second) or (first == third) or (second == third):
        if (first == second) and (second == third):
            print "You got 10 points! Full matches!"
            points = points + 10
        else:
            print "You got 5 points, 2 matched!"
            points = points + 5
    else:
        print "Sorry try again!"
    print "You now have %s points" % points
    ask()

def ask():
    play_again = raw_input ("Do you want to play again? (Yes or No):")
    if play_again == "yes" or "Yes":
        run_machine()
    elif play_again == "No" or "no":
        print "Bye!"
    else:
        print "I don't know what that means"
        ask()

run_machine()

1 Answers1

1

This does not do what you think it does.

 if play_again == "yes" or "Yes":

The correct way to say this would be the following (I added parentheses for emphasis)

if (play_again == "yes") or (play_again == "Yes"):

The way you originally wrote it means the following in pseudo-code

if (play_again == "yes")
or
if ("Yes")  # this always evaluates to True
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • alternatively: `if play_again in ('yes', 'Yes'):` but what OP would likely be happy with is `if play_again.lower() == 'yes':` – Ryan Haining May 30 '14 at 16:39
  • Oh yup that was a pretty major fluke on my part. Forgot about Syntax. Also I think implementing the case lower would make this a lot simpler as well so thanks Ryan. – user3234240 Jun 02 '14 at 16:50