Rock Paper Scissors

project
python

#1


I don't know what's wrong with it 

import random
rounds = 1 

def computer():
    choice = ["Rock", "Paper", "scissors"]
    random_choice = random.randrange(0, 3)
    print "Computer chose", choice[random_choice]
    
while rounds < 4: 
    print "Round", rounds
    print input("  Lets play rock, paper, scissors. Press 1 for rock, 2 for paper and 3 for scissors:")
    if input == 1:
        print "You chose rock"
    elif input == 2:
        print "You chose paper"
    elif input == 3:
        print "You chose scissors"
    elif input > 3:
        print "That's not an option"
        continue 
    
    if input == computer():
        print "It's a draw"
        rounds += 1 
    elif input == 1 and computer() == "scissors":
        print "You won"
        rounds += 1 
    elif input == 3 and computer() == "rock":
        print "You lose"
        rounds += 1 
    elif input == 1 and computer() == "paper":
        print "You lose"
        rounds += 1 
    elif input == 2 and computer() == "rock":
        print "You won"
        rounds += 1 
    elif input == 2 and computer() == "scissors":
        print "You lose"
        rounds += 1 
    else:
        print "You won"
        rounds += 1


#2

Is this an exercise or a personal project? If it is an exercise, please post a link. Thank you.


#3

Rock paper scissors can be broken down as follows.

rules = {
    1: 'rock',
    2: 'paper',
    3: 'scissors'
}

We need a few more things before we continue,

results = {
    -2: 'Win',
    0: 'Draw',
    1: 'Win'
}

Now we can write some code to play the game,

Python Rock Paper Scissors

from random import randint

rules = {
    1: 'rock',
    2: 'paper',
    3: 'scissors'
}

results = {
    -2: 'Win',
    0: 'Draw',
    1: 'Win'
}

def game():
    player = int(raw_input('Enter a number:\nRock: 1\nPaper: 2\nScissors: 3\n'))
    if 4 > player or player < 0: return False
    cpu = randint(1,3)
    print('You picked: {:^12}The Computer picked: {:^12}\nResulting in a {:^12}'.format(rules.get(player), rules.get(cpu), results.get(player-cpu, 'Loss')))

The above should display everything needed for a rudimentary RPS game.

Feel free to expand on this code as there is no error checking of any kind what so ever. if there are any questions, ask away.


#4

Did you mean to write, if 4 > player?


#5

Yup, didn't notice that. Thank you.


#6

Am I correct in assuming this will only work correctly in Python 3 due to the string formatting?


#7

@mtf

String formating works on both 2.* and 3.* so no worries, just have to change the raw_input to input for 3.


#8

The difference would appear to be in the padding. There is no center align in 2.*.


#9

thank you @mtf and @zeziba


#10