-2

Can anyone help me understand why my very simple rock paper scissors code gets stuck and exits at the end of line 18 please? I´ve tested each part individually and it works, it might not be the prettiest code but it seems to do the job, however in it´s latest itteration it just exits at the en of line 18, exit code 0, so no error, does´nt say anything is wrong, it just doesn´t execute the next line, like there´s a break or an exit on that line, but there isn´t:

 import random

def startgame():
    print("Please choose rock - r, paper - p or scissors - s:")
    pchoice = input(str())
    if(pchoice.lower in ["r","rock"]):
        pchoice = "0"
    elif(pchoice.lower in ["s","scissors"]):
        pchoice = "1"
    elif(pchoice.lower in ["p","paper"]):
        pchoice = "2"
    cchoice = (str(random.randint(0,2)))
    if(cchoice == "0"):
        print("Computer has chosen: Rock \n")
    elif(cchoice == "1"):
        print("Computer has chosen: Scissors \n")
    elif(cchoice == "2"):
        print("Computer has chosen: Paper \n")
#runs perfect up to here, then stops without continuing
    battle = str(pchoice + cchoice)
    if(battle == "00" and "11" and "22"):
        print("Draw! \n")
        playagain()
    elif(battle == "02" and "10" and "21"):
        if(battle == "02"):
            print("You Lose! \nRock is wrapped by paper! \n")
        elif(battle == "10"):
            print("You Lose! \nScissors are blunted by rock! \n")
        elif(battle == "21"):
            print("You Lose! \nPaper is cut by scissors! \n")
            playagain()
    elif(battle == "01" and "12" and "20"):
        if(battle == "01"):
            print("You Win! \nRock blunts scissors! \n")
        elif(battle == "12"):
            print("You Win! \nScissors cut paper! \n")
        elif(battle == "20"):
            print("You Win! \nPaper wraps rock! \n")
            playagain()

def main():
    print("\nWelcome to Simon´s Rock, Paper, Scissors! \n \n")
    startgame()

def playagain():
        again = input("Would you like to play again? y/n \n \n")
        if(again == "y"):
            startgame()
        elif(again == "n"):
            print("Thank you for playing")
            exit()
        else:
            print("Please choose a valid option...")
        playagain()

main()
Rayne
  • 119
  • 1
  • 2
  • 7
  • Try printing the value of battle, and compare it with what you have inside the consecutive if statements. – Jack Evans Aug 14 '17 at 13:51
  • Do you have an entry that duplicates the issue? – Miket25 Aug 14 '17 at 13:52
  • 3
    There are many issues with the provided code. Some already explained in the answers, some aren't. For example, everywhere you have `.lower` and `.upper` should be `lower()` and `upper()`. Otherwise these `if` conditions will never be `True`. – DeepSpace Aug 14 '17 at 13:57
  • Please see https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true – PM 2Ring Aug 14 '17 at 13:59
  • Thank you so much DeepSpace, your comments got me leaps and bounds with the program. Now it all works perfect, Python is such a grammar nazi :P – Rayne Aug 14 '17 at 22:43

2 Answers2

0

The error is in this:

if(battle == "00" and "11" and "22"):

this will evaluate to False in all cases but 00, you need to change it to:

if battle == "00" or battle == "11" or battle == "22":

And also the other two statements where you use and

Your statement gets interpreted as the following:

True/False 1- if battle == "00" 
True       2- and "11" #<-- here it checks if the string is True which means string is not empty
True       3- and "22" is True #<-- here too

So your statement will only work if all statements are True because you are using and which requires all parts of the statement to be True. Second and third parts are always True so its checking if the choice is "00"

What you need is this:

1- if battle == "00" True/False
2- or battle == "11" True/False
3- or battle == "22" True/False

And you need only one part to be True here for the statement to run because of or

Mohd
  • 5,523
  • 7
  • 19
  • 30
0

In the lines like if(battle == "00" and "11" and "22"): use in operator if(battle in ["00", "11", "22"]):

playagain() is not getting called since none of the conditions are true.