基本的摇滚,纸张或剪刀程序

时间:2012-04-17 17:01:32

标签: python

更新的代码:

  #RockPS
import random

Choices=['R','P','S']
UserScore=0
CpuScore=0
Games=0

while Games<6:
    UserChoice=input('Rock, paper or scissors? (Type R, P or S respectively)')
    if UserChoice in Choices:
        Games+=1
CpuChoice = random.choice(Choices)   

if UserChoice == 'S' and CpuChoice == 'P':
    UserScore+=1
if UserChoice == 'P' and CpuChoice == 'R':
    UserScore+=1
if UserChoice == 'R' and CpuChoice == 'S':
    UserScore+=1
if UserChoice == 'S' and CpuChoice == 'R':
    CpuScore+=1
if UserChoice == 'P' and CpuChoice == 'S':
    CpuScore+=1
if UserChoice == 'R' and CpuChoice == 'P':
    CpuScore+=1

else:
    print('Only R, P or S are allowed')


print(UserScore, CpuScore)
if UserScore>CpuScore:
    print('Well done, you won!')
if UserScore==CpuScore:
    print('You tied!')
if UserScore<CpuScore:
    ('Unlucky, you lost.')

还有一个问题。当打印分数时,它只会在每一个方向都显示为1 0,假定对于拥有大多数胜利的玩家。它应该计算每场比赛,例如3 2或4 1

4 个答案:

答案 0 :(得分:3)

可能不是唯一的问题,但是马上突然出现了什么:行

random.choice(CpuChoice)

不会将CpuChoice设置为计算机的随机选择。 random.choice()返回一个随机选择,但是你不能将它存储在任何地方。你想要

CpuChoice = random.choice(['Rock', 'Paper', 'Scissors'])

...你想在你的循环中做那个(并收集用户输入,并可能输出每个回合的结果),除非CPU每次都做同样的选择,这将使它很容易失败:)

此外,你永远不会在任何地方增加游戏 - 实际上,你可能想要一个for循环,因为你只想运行6次主体。

答案 1 :(得分:1)

您需要在内输入。不要忘记增加比赛次数。

答案 2 :(得分:1)

您的代码中存在许多问题:

  1. 您没有正确使用random.choice
  2. 每次用户播放前都应该调用该功能。如果你打电话一次,它每次都会保持不变。
  3. 要求用户输入一次值,因为input(...)函数在循环时调用。
  4. 这不是命名变量的方法(至少在Python中)
  5. 使用if / elif多次执行同样的操作不是使用or
  6. 的方式

    所以,你的代码看起来应该是这样的:

    #RockPS
    import random
    
    game_choices = ['R','P','S']
    user_score = 0
    cpu_score = 0
    games = 0
    
    while games<6:
        user_choice = input('Rock, paper or scissors? (Type R, P or S respectively)')
        if user_choice in game_choices:
            games += 1
            cpu_choice = random.choice(cpu_choices)
            if (user_choice == 'S' and cpu_choice == 'P') or \
               (user_choice == 'P' and cpu_choice == 'R') or \
               (user_choice == 'R' and cpu_choice == 'S'):
                user_score += 1
            elif (user_choice == 'S' and cpu_choice == 'R') or \
                 (user_choice == 'P' and cpu_choice == 'S') or \
                 (user_choice == 'R' and cpu_choice == 'P'):
                cpu_score += 1
        else:
            print('Only R, P or S are allowed')
    
    print(user_score, cpu_score)
    if user_score>cpu_score:
        print('Well done, you won!')
    elif user_score == cpu_score:
        print('You tied!')
    elif user_score<cpu_score:
        print('Unlucky, you lost.')
    

    你仍然可以改进它。我添加了一张支票,以确保这些字母是RPS。而这一大块条件,你可以通过使用一个返回获胜者的函数来缩短它们(例如,如果cpu获胜则为1,如果玩家获胜则为0)等等......

答案 3 :(得分:0)

在这里,我为你修好了。

我试图模仿你的风格,并保持我所能做到的,以便让你看到werde所做的改变。 这就是说:这种风格通常是气馁的,请参阅jadkik94的答案以获得更好的风格。 也许我稍后会在编辑中重写这个程序。如果我找时间的话。

import random
CpuChoices=['Rock','Paper','Scissors']
PlayerChoices = dict(zip("RPS", CpuChoices))
UserScore=0
CpuScore=0
Games=0
while Games<6:
    UserChoice=input('Rock, paper or scissors? (Type R, P or S respectively)')
    if UserChoice not in PlayerChoices: continue
    CpuChoice = random.choice(CpuChoices)

    if UserChoice=='S' and CpuChoice=='Paper':
        UserScore+=1
    elif UserChoice=='P' and CpuChoice=='Rock':
        UserScore+=1
    elif UserChoice=='R' and CpuChoice=='Scissors':
        UserScore+=1

    if UserChoice=='S' and CpuChoice=='Rock':
        CpuScore+=1 
    elif UserChoice=='P' and CpuChoice=='Scissors':
        CpuScore+=1
    elif UserChoice=='R' and CpuChoice=='Paper':
        CpuScore+=1
    print("CPU chose %s against your %s" % (CpuChoice, PlayerChoices[UserChoice]))
    print("User: %s - CPU: %s" % (UserScore, CpuScore))
    Games += 1

if UserScore > CpuScore:
    print('Well done, you won!')
if UserScore == CpuScore:
    print('You tied!')
if UserScore < CpuScore:
    print('Unlucky, you lost.')

print("the only winning move it not to play")
承诺,这是我编写代码的代码(好吧,不是真的,但是......你明白了):

import random

class RSPHand(object):
    names = ('Rock', 'Paper', 'Scissors')
    beats = {
            'Rock': 'Scissors',
            'Scissors': 'Paper',
            'Paper': 'Rock',
            }
    def __init__(self, name):
        if name not in self.__class__.names:
            try:
                name = [n for n in self.__class__.names if n[0] == name.upper()][0]
            except IndexError:
                raise ValueError ("Name not valid")
        self.name = name
        self.shortname = self.name[0]
    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.name)
    def __str__(self):
        return self.name
    def __gt__(self, other):
        return other.name == self.__class__.beats[self.name]
    def __lt__(self, other):
        return self.name == self.__class__.beats[other.name]

choices=[]
for name in RSPHand.names:
    choices.append(RSPHand(name))


playerscore = cpuscore = 0

while True:
    rounds = input("Best out of how many rounds? ")
    try:
        rounds = int(rounds)
    except ValueError:
        continue
    break

while rounds:
    playerchoice = input("%s? (Type first letter, lowercase or not, or full name) " % [choice.name for choice in choices])
    try:
        playerchoice = RSPHand(playerchoice)
    except ValueError:
        continue
    cpuchoice = random.choice(choices)
    print ("CPU chose %s against your %s" % (cpuchoice, playerchoice))
    if playerchoice < cpuchoice:
        cpuscore += 1
        print("too bad for you")
    elif playerchoice > cpuchoice:
        playerscore += 1
        print("too bad for CPU")
    else:
        print("that's a tie for this round")
    print ("CPU: %s - Player: %s" % (cpuscore, playerscore))
    rounds -= 1

if playerscore > cpuscore:
    print('Well done, you won!')
elif playerscore == cpuscore:
    print('You tied!')
elif playerscore < cpuscore:
    print('Unlucky, you lost.')