Rock, Paper, Scissor-python-code. Help Simplify
Solution 1:
While stackoverflow is not really meant as a learning platform, here are some advises:
- Read the ZEN (type
import this
into your python console). - In your particular case, plenty of conditions are usually a bad idea.
At the very least, all TIE conditions can be thrown together:
if choice == cpu_choice:
# TIE
Throw in some grammar:
names = ['rock', 'paper', 'scissors']
print("Computer chooses {}, you loose".format(names[cpu_choice]))
Essentially, there are only three conditions:
wins, losses = 0, 0
for round in range(10):
# Your choice and CPU choice
cpu_wins = (cpu_choice > choice or (choice == 3 and cpu_choice == 1))
tie = (cpu_choice == choice)
if cpu_wins:
# You loose
print("Computer chooses {}, you loose".format(names[cpu_choice]))
losses += 1
if not cpu_wins and tie:
# tie
else:
# you win
Also, you don't even use the variables p
, r
and s
defined above....
Solution 2:
Some suggestions:
All your conditional cases contain round variable increasing, except when wrong data input occured, so you can bring round += 1 lines out upper, and decrrease round variable only once in else case
You have if cases that do same jobs, e.g. when 'TIE!' happened; it's better to group such cases. 'TIE!' cases can be grouped under one condition choice == cpu_choice thus ommiting 3 elif clauses. Think about the same problem in other game cases.
Use better code formatting, e.g. what PEP-8 standard suggests.
Solution 3:
You can determine whether the player wins using modulo arithmetic:
player_result = ["tie", "win", "lose"]
player_choice = input('(1)rock, (2)paper, or (3)scissors? :')
cpu_choice= random.randint(1, 3)
result = player_result[(player_choice - cpu_choice) % 3]
print "You " + result
if result == "win":
wins += 1
elif result == "lose":
loses += 1
Solution 4:
I would do something like this, which may be a bit above your level, but If you research how this code works, you will be much better at pythoN! :)
from random import randint
def do_rounds(num_rounds):
choice_dict = {1: 'rock', 2: 'paper', 3: 'scissors'}
beats_dict = {1: 3, 2: 1, 3: 2}
for round in range(num_rounds):
computer_choice = randint(1, 3)
while True:
player_choice = raw_input('(1)rock, (2)paper, or (3)scissors? :')
if player_choice in ("1", "2", "3"):
player_choice = int(player_choice)
break
else:
print "input must be an integer 1, 2 or 3"
player_lost = beats_dict[computer_choice] == player_choice
tie = 1 if computer_choice == player_choice else 0
win = 0 if player_lost else 1
loss = 1 if player_lost else 0
print "computer picked: %s" % choice_dict[computer_choice],
print " you picked: %s" % choice_dict[player_choice]
yield tie, win, loss
def run_game():
ties, wins, losses = zip(*do_rounds(4))
ties, wins, losses = sum(ties), sum(wins), sum(losses)
print "ties = %s, wins = %s, losses = %s" % (ties, wins, losses)
if wins > losses:
print "you won!"
elif wins == losses:
print "tie!"
else:
print "loser!!!"
if __name__ == "__main__":
run_game()
"""
(1)rock, (2)paper, or (3)scissors? :3
computer picked: rock, you picked: scissors
(1)rock, (2)paper, or (3)scissors? :2
computer picked: paper, you picked: paper
(1)rock, (2)paper, or (3)scissors? :1
computer picked: paper, you picked: rock
(1)rock, (2)paper, or (3)scissors? :3
computer picked: rock, you picked: scissors
ties = 1, wins = 1, losses = 3
loser!!!
"""
Post a Comment for "Rock, Paper, Scissor-python-code. Help Simplify"