MoRayhxn
MoRayhxn

Reputation: 17

Refining/condensing my python code for rock paper scissors (I just started learning)

### Import specification function required - for some reason if I do just "import random"
from random import randint

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

### While pretty much is used so we can play over and over.
while True:
    computer = moves[randint(0,2)]
    player = input("Choose rock, paper or scissors, or 'end' to finish the game: ").lower()

### Break the loop if player wants to end
    if player == "end":
        print("The game is over")
        break

### All possible iterations.
    elif player == computer:
        print("It's a tie!")
    elif player == "rock":
        if computer == "paper":
            print("You lose!", computer, "beats", player)
        else:
            print("You win!", player, "beats", computer)
    elif player == "paper":
        if computer == "scissors":
            print("You lose!", computer, "beats", player)
        else:
            print("You win!", player, "beats", computer)
    elif player == "scissors":
        if computer == "rock":
            print("You lose!", computer, "beats", player)
        else:
            print("You win!", player, "beats", computer)

### This is to let the player know they typed in the wrong thing and re do it.
    else: 
        print("Check your spelling and try again")



I made this code and have tried other ways to simplify but none of them seem to work as intended.

Any help refining/condensing the code with some explanations/guidance would be much appreciated.

This is my 3rd-day learning python so I'm not familiar with any of the more advanced python commands you may know.

Upvotes: 0

Views: 55

Answers (3)

oribro
oribro

Reputation: 26

You can create a function as follows to reduce code repetition in the win condition logic:

def is_win(player, computer):
    if player == computer:
        print('Tie')
    elif player == 'rock' and computer == 'scissors' or
player == 'paper' and computer == 'rock' or
# Rest of win conditions here
        print('Win')
    else:
        print('Lose')

Then invoke it like this:

is_win(player, computer)

There is another a bit advanced option to use class for Player and class for Computer and define methods for win for each one.

Upvotes: 0

Link_jon
Link_jon

Reputation: 26

There isn't much here to be refined, really (and looks like something I'd make in Lua lol).

However, there are a few quality of life suggestions I can give:

player = input("...")
player = player.lower()  # -- Strings in python act akin to arrays, and this changes all
                # -- characters in the string lowercase

This way, if someone capitalizes in their response, it will still be recognized. Beyond that, perhaps adding a simpler method to responding aswell:

elif player == "rock" or player == "r"

elif player == "paper" or player == "p"

elif player == "scissors" or player == "s"

However, that does add complexity, and I'm not sure if your looking to lower line count or complexity. (Probably both.) It's your choice.

(Maybe arrays could be used to make it very easy to add other moves, but I personally don't see the point there...)

Upvotes: 0

blackbrandt
blackbrandt

Reputation: 2095

You can eliminate most of your code and achieve the same result: At its core, rock/paper/scissors is a 1/3 chance of each of win, loss, or tie. Therefore, requesting the user's input and then returning a randomly chosen outcome will give the same results.

import random as r
input("Enter rock, paper, or scissors")
print(r.choice(["You won!", "You lost!", "Tie!"]))

Upvotes: 1

Related Questions