user16188627
user16188627

Reputation:

Trying to print "tie" for rock paper scissors game but the game always gives the win to the computer

In my Python class we are making a Rock, Paper, Scissors game but every time the game is supposed to tie, it gives the computer the win. Its obviously supposed to print a tie but prints computer wins! instead. How do I fix it? Here is my code:

#computer choice code.  Copy and paste the code from the instructions.
import random
computerChoice = random.randint(1,3)

#player choice code
playerChoice = int(input("Please choose a number between 1 and 3: "))

#who won?
if (playerChoice == computerChoice):
    result = """
  _______ _      _ 
 |__   __(_)    | |
    | |   _  ___| |
    | |  | |/ _ \ |
    | |  | |  __/_|
    |_|  |_|\___(_)
                   
                   """
if (playerChoice == 1 and computerChoice == 3 or \
    playerChoice == 2 and computerChoice == 1 or \
    playerChoice == 3 and computerChoice == 2):
        result = """
 __     __          __          ___       _ 
 \ \   / /          \ \        / (_)     | |
  \ \_/ /__  _   _   \ \  /\  / / _ _ __ | |
   \   / _ \| | | |   \ \/  \/ / | | '_ \| |
    | | (_) | |_| |    \  /\  /  | | | | |_|
    |_|\___/ \__,_|     \/  \/   |_|_| |_(_)
                                            
                                            """
else:
    result = """
   _____ _____  _    _  __          ___           _ 
  / ____|  __ \| |  | | \ \        / (_)         | |
 | |    | |__) | |  | |  \ \  /\  / / _ _ __  ___| |
 | |    |  ___/| |  | |   \ \/  \/ / | | '_ \/ __| |
 | |____| |    | |__| |    \  /\  /  | | | | \__ \_|
  \_____|_|     \____/      \/  \/   |_|_| |_|___(_)
                                                    
                                                    """
    
print(result)

Upvotes: 0

Views: 159

Answers (1)

lejlot
lejlot

Reputation: 66805

The reason is your second "if" (or the "else" part) always executes, consequently the "result" gets always overwritten with "You win" or "CPU wins".

To fix it, just change it to elif

if (playerChoice == computerChoice):
    result = """
  _______ _      _ 
 |__   __(_)    | |
    | |   _  ___| |
    | |  | |/ _ \ |
    | |  | |  __/_|
    |_|  |_|\___(_)
                   
                   """
elif (playerChoice == 1 and computerChoice == 3 or \
    playerChoice == 2 and computerChoice == 1 or \
    playerChoice == 3 and computerChoice == 2):
        result = """
 __     __          __          ___       _ 
 \ \   / /          \ \        / (_)     | |
  \ \_/ /__  _   _   \ \  /\  / / _ _ __ | |
   \   / _ \| | | |   \ \/  \/ / | | '_ \| |
    | | (_) | |_| |    \  /\  /  | | | | |_|
    |_|\___/ \__,_|     \/  \/   |_|_| |_(_)
                                            
                                            """
else:
    result = """
   _____ _____  _    _  __          ___           _ 
  / ____|  __ \| |  | | \ \        / (_)         | |
 | |    | |__) | |  | |  \ \  /\  / / _ _ __  ___| |
 | |    |  ___/| |  | |   \ \/  \/ / | | '_ \/ __| |
 | |____| |    | |__| |    \  /\  /  | | | | \__ \_|
  \_____|_|     \____/      \/  \/   |_|_| |_|___(_)
                                                    
                                                    """
    
print(result)

Upvotes: 2

Related Questions