岩石剪刀纸程序

2024-05-14 13:59:47 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是用python编写石头剪刀布游戏的代码。 如果我运行这个代码,它可以工作,但是,当它变成tie时,它输出如下所示。 当我得到平局的结果时,有没有什么办法可以消除印花(圆形)? 我希望它看起来像示例底部所示

****************第1轮****************

选择你的投球,还是投手?p 领带!在

****************第1轮****************

选择你的投球,还是投手?s 电脑把石头砸了,你输了!在

你的分数:0 电脑成绩:1分

****************第三轮

选择你的投球,还是投手?s 领带!在

选择你的投球,还是投手?p 领带!在

选择你的投球,还是投手?r 电脑扔剪刀,你赢了!在

你的分数:2 电脑成绩:1分

# A Python program for the Rock, Paper, Scissors game. 
import random

def rock_paper_scissors():
''' Write your code for playing Rock Paper Scissors here. '''
user = 0
computer = 0
rounds = 1   

print()
score = (int(input('How many points does it take to win? ')))
print()

while (computer < score and user < score):
    RPS = random.randint(0,2)
    if (RPS == 0):
        RPS = 'rock'
    elif (RPS == 1):
        RPS = 'paper'
    elif(RPS == 2):
        RPS = 'scissors'


    print('*'*21 + 'ROUND #'+str(rounds) + '*'*21)
    print()
    player = (input('Pick your throw: [r]ock, [p]aper, or [s]cissors? '))


    if  RPS == 'rock' and player == 'r':
        print('Tie!')

    elif RPS == 'rock' and player == 's':
        print('Computer threws rock, you lose!')
        computer+=1
        rounds += 1
        print()
        print('Your Score: ',user)
        print('Computer Score: ',computer)
    elif RPS == 'rock' and player == 'p':
        print('Computer threw rock, you win!')
        user+=1
        rounds +=1
        print()
        print('Your Score: ',user)
        print('Computer Score: ',computer)            
    if  RPS == 'paper' and player == 'p':
        print('Tie!')
    elif RPS == 'paper' and player == 'r':
        print('Computer threw paper, you lose!')
        computer +=1
        rounds += 1
        print()
        print('Your Score: ',user)
        print('Computer Score: ',computer)            
    elif RPS == 'paper' and player == 's':
        print('Computer threw paper, you win!')
        user +=1
        rounds +=1
        print()
        print('Your Score: ',user)
        print('Computer Score: ',computer)        
    if RPS == 'scissors' and player == 's':
        print('Tie!')
    elif RPS == 'scissors'and player == 'p':
        print('Computer threw scissors, you lose!')
        computer +=1
        rounds+=1
        print()
        print('Your Score: ',user)
        print('Computer Score: ',computer)            
    elif RPS == 'scissors' and player == 'r':
        print('Computer threw scissors, you win!')
        user +=1
        rounds+=1
        print()
        print('Your Score: ',user)
        print('Computer Score: ',computer)            
    print()

if user> computer:
    print('*'*21 + 'GAME OVER' + '*'*21)
    print('You win!')
else:
    print('*'*21 + 'GAME OVER' + '*'*21)
    print('Computer win!')

print()        

def main(): 
print('ROCK PAPER SCISSORS in Python')
print()
print('Rules: 1) Rock wins over Scissors.')
print('       2) Scissors wins over Paper.')
print('       3) Paper wins over Rock.')

rock_paper_scissors()

main()

Tags: andyouwincomputerpaperscoreplayerprint
2条回答

因此,我认为您有许多方面可以简化代码,但为了快速解决问题,请在下面这一行:

print('*'*21 + 'ROUND #'+str(rounds) + '*'*21)
print()

更改为:

^{pr2}$

在所有的情况下,如果tie场景是真的,则添加一行previous_round_was_tie = True。您还必须创建布尔值,并在while循环之外将其设置为False。在

我建议创建一个boolean was_tied,在程序开始时将其设置为等于False,并在每个可能结果的末尾将其设置为True或{}。然后可以将打印循环代码放在if not was_tied语句中。在

相关问题 更多 >

    热门问题