1

Im new to programming and trying to build a simple rock paper scissors program in python 2.7. in my function i have 2 main if statements

rules = raw_input("Before we being playing would you like to hear the rules first? ")
if rules.lower() == "yes":
    print """ 
    Scissors cuts Paper 
    Paper covers Rock 
    Rock crushes Scissors"""

and the second

choice = raw_input("please enter your choice? (Must be either rock, paper or scissors) ")
computer = random.choice(["rock", "paper", "scissors"])

if choice == "rock" or "paper" or "scissors": 
    if choice == computer : 
        print "Its a tie !"
    elif choice == "rock" and computer == "scissors": 
        print "Rock beats scissors you win!."

    elif choice == "rock" and computer == "paper": 
        print "Paper beats rock you loose !."

    elif choice == "paper" and computer == "scissors": 
        print "Scissors beats paper you loose !."

    elif choice == "paper" and computer == "rock": 
        print "Paper beats rock you win !."

    elif choice == "scissors" and computer == "paper": 
        print "Scissors beats paper you win !."

    elif choice == "scissors" and computer == "rock": 
        print "Rock beats scissors you loose !."
else : 
    print "Invalid Entry Please try again."

individually both bits of code work as they should but when i try and put them together in one function the first if statement asking about rules works but then quits before the second if statement which has the main functionality of the program. Ive tried indenting the second bit of code within the first if statement but it doesn't seem to work

I was wondering if there is anyway of making these 2 snips of code work in a simple function ? or should i create a class with these 2 functions ? also if anyone has any tips on how to make my progam better please let me know. Thanks for any help in advance.

heres the full code

import random 

def rock_paper_scissors_spock():

    rules = raw_input("Before we being playing would you like to hear the rules first? ")
    if rules.lower() == "yes":
        print """ 
        Scissors cuts Paper 
        Paper covers Rock 
        Rock crushes Scissors"""

    choice = raw_input("please enter your choice? (Must be either rock, paper or  scissors) ")
    computer = random.choice(["rock", "paper", "scissors"])

    if choice == "rock" or "paper" or "scissors": 
        if choice == computer : 
            print "Its a tie !":

        elif choice == "rock" and computer == "scissors": 
            print "Rock beats scissors you win!."

        elif choice == "rock" and computer == "paper": 
            print "Paper beats rock you loose !."

        elif choice == "paper" and computer == "scissors": 
            print "Scissors beats paper you loose !."

        elif choice == "paper" and computer == "rock": 
            print "Paper beats rock you win !."

        elif choice == "scissors" and computer == "paper": 
            print "Scissors beats paper you win !."

        elif choice == "scissors" and computer == "rock": 
            print "Rock beats scissors you loose !."
    else : 
        print "Invalid Entry PLease try again." 


rock_paper_scissors_spock()
atlantis.pd
  • 68
  • 1
  • 9
  • You have a colon (:) after a print statement, is it a typo? `print "Its a tie !":` – Selcuk Feb 13 '16 at 11:48
  • When I get rid of the SyntaxError mentioned by Selcuk your code runs fine for me on Python 2.6.6. However, it does have some logic errors: `if choice == "rock" or "paper" or "scissors":` doesn't do what you think it does. – PM 2Ring Feb 13 '16 at 11:51
  • 1
    See [Why does `a == b or c or d` always evaluate to True?](http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) (and the linked question) for an explanation, and examples showing how to fix your `if` statement. – PM 2Ring Feb 13 '16 at 11:57
  • Thanks for your help. print "Its a tie !": was a typo sorry about that and i changed the if choice like you mentioned and it works now. Thanks again. – atlantis.pd Feb 13 '16 at 12:03
  • is there anyway to make the program restart itself ie if i ask at the end of the program raw_input("do you want to play again?" ) how do i make it so it restarts the program again if they answer yes ?? – atlantis.pd Feb 13 '16 at 12:06
  • You can use a `while` loop to do that. Experiment with it for a while and if you can't get it to work properly ask a new question. Read about `while` in the docs and also check out `break` and `continue`. – PM 2Ring Feb 13 '16 at 12:08
  • Another way to make the program loop is to add `execfile(__file__)` to the end of the program. – zondo Feb 14 '16 at 13:57

1 Answers1

2

You said if choice == "rock" or "paper" or "scissors":, but Python does not connect the choice == to all of the choices. You could put parentheses around choice == "rock", and it would do the same thing. Change it to if choice in ("rock", "paper", "scissors")

zondo
  • 19,901
  • 8
  • 44
  • 83