0

更新代码:

  #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

4 回答 4

3

可能不是唯一的问题,但我马上跳出来的问题:这条线

random.choice(CpuChoice)

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

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

...并且您想在循环中这样做(并收集用户输入,并可能输出每一轮的结果),除非 CPU 每次都做出相同的选择,这将很容易击败:)

此外,您永远不会在任何地方增加 Games —— 实际上,您可能需要一个 for 循环,而不是那里的 while 循环,因为您只想运行主体 6 次。

于 2012-04-17T17:05:16.283 回答
1

你的代码有很多问题:

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

因此,您的代码应如下所示:

#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),等等......

于 2012-04-17T17:24:00.807 回答
1

您需要在循环内输入。不要忘记增加所玩游戏的数量。

于 2012-04-17T17:03:17.280 回答
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.')
于 2012-04-17T17:33:16.673 回答