Trevor Ellis
Trevor Ellis

Reputation: 3

rock paper scissors lizard spock, Python

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()

`

Upvotes: 0

Views: 8532

Answers (2)

Ofer Arial
Ofer Arial

Reputation: 1139

To summarize all of the things you should pay attention to:

  1. boolean conditions - the result changes with the parentheses that are inside the condition.

    if True or (True and False) --> this basically calculates the True and False part first (like in regular math) and then you have True or False which evaluates to True.

    if True or True and False --> this basically calculates the True or True part first (like in regular math) and then you have True and False which evaluates to False - because you don't use parentheses.

  2. Do not call a function within the same function - this is called recursion, and it's not relevant for what you need. Use a while loop, that runs as long as i.e. - you didn't get a proper choice input (while choice!='n' and choice!='y':).

  3. 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 the instructions function.

Upvotes: 2

Amir Taboul
Amir Taboul

Reputation: 31

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 

Upvotes: 2

Related Questions