I am creating a rock paper scissors lizard spock game in Python for my class and i am trying to figure out why whatever choice I make I am always winning even though I set up all my if statements correct. `
import random def instructions(): play = input("Would you like to play Rock, Paper, Scissors, Lizard, Spock(y/n): ").lower() if play == "y": print("1.Rock") print("2.Paper") print("3.Scissors") print("4.Lizard") print("5.Spock") elif play != "n": print("error has accured please type y for yes or n for no:") instructions() def getPlayerChoice(): choice = int(input("What is your choice user?: ")) if choice > 5: print("Invalid number please try again....") getPlayerChoice() elif choice < 1: print("Invalid number please try again....") getPlayerChoice() elif choice == 1: print("You picked Rock") elif choice == 2: print("You picked Paper") elif choice == 3: print("You picked Scissors") elif choice == 4: print("You picked Lizard") elif choice == 5: print("You picked Spock") return choice def getCPUChoice(): choice = random.randint(1,5) if choice == 1: print("CPU picked Rock") elif choice == 2: print("CPU picked Paper") elif choice == 3: print("CPU picked Scissors") elif choice == 4: print("CPU picked Lizard") elif choice == 5: print("CPU picked Spock") return choice def winner(playerChoice, CPUChoice, playerWins, CPUWins, ties): if playerChoice == 1 and CPUChoice == 3 or CPUChoice == 4: print("Player wins.") playerWins = playerWins.append(1) elif playerChoice == 2 and CPUChoice == 1 or CPUChoice == 5: print("Player wins.") playerWins = playerWins.append(1) elif playerChoice == 3 and CPUChoice == 2 or CPUChoice == 4: print("Player wins.") playerWins = playerWins.append(1) elif playerChoice == 4 and CPUChoice == 2 or CPUChoice == 5: print("Player wins.") playerWins = playerWins.append(1) elif playerChoice == 5 and CPUChoice == 1 or CPUChoice == 3: print("Player wins.") playerWins = playerWins.append(1) elif playerChoice == CPUChoice: print("Tie") ties = ties.append(1) else: print("CPU won") CPUWins = CPUWins.append(1) return def gameTotal(playerWins, CPUWins, ties): playerWins = sum(playerWins) CPUWins = sum(CPUWins) ties = sum(ties) print("Player final score: ", playerWins) print("CPU final Score: ", CPUWins) print("Total ties: ",ties) def main(): playerChoice = 0 playerWins = [] CPUChoice = 0 CPUWins = [] ties = [] finalPlayerWins = 0 finalCPUWins = 0 finalTies = 0 Continue = 'y' instructions() while Continue == 'y': playerChoice = getPlayerChoice() CPUChoice = getCPUChoice() winner(playerChoice,CPUChoice,playerWins, CPUWins, ties) Continue = input("Would you like to play again (y/n):").lower() if Continue == 'n': print("Printing final scores.") break gameTotal(playerWins, CPUWins, ties) main() `
42 Answers
To summarize all of the things you should pay attention to:
boolean conditions - the result changes with the parentheses that are inside the condition.
if True or (True and False)--> this basically calculates theTrue and Falsepart first (like in regular math) and then you haveTrue or Falsewhich evaluates to True.if True or True and False--> this basically calculates theTrue or Truepart first (like in regular math) and then you haveTrue and Falsewhich evaluates to False - because you don't use parentheses.Do not call a function within the same function - this is called
recursion, and it's not relevant for what you need. Use awhileloop, that runs as long as i.e. - you didn't get a proper choice input (while choice!='n' and choice!='y':).Your instructions choice - the choice made by the user doesn't really change the flow of the game. the game starts also if the user entered
no. You should add an exit statement in theinstructionsfunction.
The reason is you are missing parentheses on all of the "if" conditions.
if False and True or True # =True if False and (True or False) # =False