更新代码:
#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.')
还有一个问题。当分数被打印出来时,每种情况下都只显示10分,这意味着大多数获胜的玩家。它应该计算每场比赛,例如32或41
您的代码中存在许多问题:
random.choice
。在input(...)
函数在循环的外部被调用一次。在or
所以,你的代码应该是这样的:
你还是可以改进的。我加了一张支票,确保字母不是RPS就是RPS。而这一大块的条件,你可以通过使用一个返回赢家的函数使它们变短(例如,如果cpu赢了,则返回1,如果玩家赢了,则返回0),等等。。。在
你需要在你的循环里输入。别忘了增加玩游戏的次数。在
可能不是唯一的问题,但我马上想到的是:排队
不会将CpuChoice设置为计算机的随机选择。随机选择()返回一个随机选择,但不会将其存储在任何位置。你想要吗
^{pr2}$…并且您希望在循环内执行此操作(收集用户输入,并可能输出每轮的结果),除非CPU每次都执行相同的选择,这将很容易被击败:)
你也不会在任何地方增加游戏——实际上,你可能需要一个for循环,因为你只想运行6次。在
相关问题 更多 >
编程相关推荐