views:

1148

answers:

3

I need help with a python game im working on (I just started learning Python about 3 days ago, so I'm still a nOob =)

This is what I came up with:

import random 

from time import sleep 

print "Please select: " 
print "1  Rock" 
print "2  Paper" 
print "3  Scissors" 

player = input ("Choose from 1-3: ") 

if player == 1: 
    print "You choose Rock" 
    sleep (2) 
    print "CPU chooses Paper" 
    sleep (.5) 
    print "You lose, and you will never win!" 

elif player == 2: 
    print "You choose Paper" 
    sleep (2) 
    print "CPU chooses Scissors" 
    sleep (.5) 
    print "You lose, and you will never win!" 

else: 
    print "You choose Scissors" 
    sleep (2) 
    print "CPU chooses Rock" 
    sleep (.5) 
    print "You lose, and you will never win!"

and what I want the program to do is to RANDOMLY choose 1 out of the three options (rock paper scissors) no matter what the user inputs!

+24  A: 

Well, you've already imported the random module, that's a start.

Try the random.choice function.

>>> from random import choice
>>> cpu_choice = choice(('rock', 'paper', 'scissors'))
sykora
+6  A: 
import random

ROCK, PAPER, SCISSORS = 1, 2, 3
names = 'ROCK', 'PAPER', 'SCISSORS'

def beats(a, b):
    if (a,b) in ((ROCK, PAPER), (PAPER, SCISSORS), (SCISSORS, ROCK)): 
        return False

    return True


print "Please select: " 
print "1  Rock" 
print "2  Paper" 
print "3  Scissors" 

player = int(input ("Choose from 1-3: "))
cpu = random.choice((ROCK, PAPER, SCISSORS))

if cpu != player:
    if beats(player, cpu):
        print "player won"
    else:
        print "cpu won"
else:
    print "tie!"

print names[player-1], "vs", names[cpu-1]
gumuz
Neat! I had to make one too. =)
PEZ
Your beats function is a little clunky... why not just "return (a,b) in ((PAPER, ROCK), (SCISSORS, PAPER), (SCISSORS, ROCK))"?
Kiv
+3  A: 

Inspired by gumuz:

import random

WEAPONS = 'Rock', 'Paper', 'Scissors'

for i in range(0, 3):
  print "%d %s" % (i + 1, WEAPONS[i])

player = int(input ("Choose from 1-3: ")) - 1
cpu = random.choice(range(0, 3))

print "%s vs %s" % (WEAPONS[player], WEAPONS[cpu])
if cpu != player:
  if (player - cpu) % 3 < (cpu - player) % 3:
    print "Player wins"
  else:
    print "CPU wins"
else:
  print "tie!"
PEZ