Rock-Paper-Scissors python script fails

Viewed 79

Am a beginner at python. The below is my script for rock-paper-scissors. The intended output is to throw "You Win" or "You lose" or "Draw" as per the rules. However, it throws "You Lose" even when I actually win as per the given below logic.

Where am I going wrong at? Can someone kindly guide me here?

Program:

# Rock Paper and Scissors Game

import random

rock = ""
paper = ""
scissors = "✂"
list = [rock , paper, scissors]

player_chooses = input("What do you choose ? ").lower()

computer_chooses = random.choice(list)

print("The computer choose :", computer_chooses)

if player_chooses == rock and computer_chooses == scissors:
    print("You Win")
elif player_chooses == paper and computer_chooses == rock:
    print("You Win")
elif player_chooses == scissors and computer_chooses == paper:
    print("You Win")
elif player_chooses == computer_chooses:
    print("Draw")
else:
    print("You Lose")
3 Answers

I know that I'm arriving late (1 month from the publication to be exact) but your problem is that you are comparing a string (that would be an array of characters in terms of code) against the string that represent each Unicode symbols that you are using ༼ つ ◕_◕ ༽つ.

But I love your idea of using those symbols, so why don't we keep them inside a function that will return the chosen symbol at the right time according to the random.choice() function?

import random

def convert(string):
    if string == 'rock':
        return ""
    elif string == "paper":
        return ""
    elif string == "scissors":
        return "✂"
    else:  
        print("An error has raised", "Program will Stop")
        exit()

choices = ["rock" , "paper", "scissors"]

player_chooses = input("What do you choose ? ").lower()

computer_chooses = random.choice(choices)

print("The computer choose :", convert(computer_chooses))

# --- Watch tip below the code --- #

if player_chooses == computer_chooses:
    print("Draw")
elif player_chooses == "rock" and computer_chooses == "scissors":
    print("You Win")
elif player_chooses == "paper" and computer_chooses == "rock":
    print("You Win")
elif player_chooses == "scissors" and computer_chooses == "paper":
    print("You Win")
else:
    print("You Lose")

Giving a little tip here that you should keep in mind (⌐■_■): 

Logical choices are expensive (really expensive if you are using a code that will summon them lot of time) here of course you almost no logical choices but you should be careful with the order that you write them as that will be the order that they will be made.

Here, probability speaking, you have 1/4 chances to get any of the results so the most efficient way of coding this would be asking first for the only option that only has one comparison (that's why I moved it from the last position to the first).

As I said before, this was just a comment so you can learn a better practice for the next code you write! (And an even much more computational efficient way of making these comparisons would be implementing a switch, I encourage you to google how to make use of switches in python as they are not built-in the language, but there are few clever ways to use them through dictionaries!)

Good luck and keep fighting. Python is an amazing language, I'm sure you will love it!

I don't quite understand why you add those icon for the rock, paper, scissors since the console won't print out picture for you to choose just from the icon you paste in, so those can be the errors. Perhaps just use a simple string instead such as

option = ["rock", "paper", "scissors"]
computer_choose = random.choice(option)

You have to write rock, paper scissors between double quote everywhere. Since it is a string. And in python language we use "" or '' to denote strings.

This is the corrected code ->

# Rock Paper and Scissors Game

import random

rock = ""
paper = ""
scissors = "✂"
computer_option = ["rock" , "paper", "scissors"]

computer_chooses = random.choice(computer_option)

player_chooses = input("What do you choose ? ").lower()

print("The computer choose :", computer_chooses)

if player_chooses == "rock" and computer_chooses == "scissors":
    print("You Win")
elif player_chooses == "paper" and computer_chooses == "rock":
    print("You Win")
elif player_chooses == "scissors" and computer_chooses == "paper":
    print("You Win")
elif player_chooses == computer_chooses:
    print("Draw")
else:
    print("You Lose")
Related