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:
ifchoice==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, 0forroundinrange(10):
# Your choice and CPU choice
cpu_wins = (cpu_choice > choice or (choice == 3and cpu_choice == 1))
tie = (cpu_choice == choice)
if cpu_wins:
# You looseprint("Computer chooses {}, you loose".format(names[cpu_choice]))
losses += 1ifnot cpu_wins and tie:
# tieelse:
# 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
defdo_rounds(num_rounds):
choice_dict = {1: 'rock', 2: 'paper', 3: 'scissors'}
beats_dict = {1: 3, 2: 1, 3: 2}
forroundinrange(num_rounds):
computer_choice = randint(1, 3)
whileTrue:
player_choice = raw_input('(1)rock, (2)paper, or (3)scissors? :')
if player_choice in ("1", "2", "3"):
player_choice = int(player_choice)
breakelse:
print"input must be an integer 1, 2 or 3"
player_lost = beats_dict[computer_choice] == player_choice
tie = 1if computer_choice == player_choice else0
win = 0if player_lost else1
loss = 1if player_lost else0print"computer picked: %s" % choice_dict[computer_choice],
print" you picked: %s" % choice_dict[player_choice]
yield tie, win, loss
defrun_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"