岩石剪纸程序

时间:2016-10-03 03:20:18

标签: python

以下是在python中编写岩石剪刀纸游戏的代码。 如果我运行代码,它可以工作但是,当它变成平局时,它会像这样输出。 无论如何,当我得到领带的结果时,我可以消除打印(圆形)吗? 我希望看起来像示例底部所示

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

选择你的投掷:[r] ock,[p] aper还是[s] cissors? p 扎!

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

选择你的投掷:[r] ock,[p] aper还是[s] cissors?小号 电脑扔石头,你输了!

你的分数:0 计算机得分:1

*********************第3轮*********************

选择你的投掷:[r] ock,[p] aper还是[s] cissors?小号  扎!

选择你的投掷:[r] ock,[p] aper还是[s] cissors? p  扎!

选择你的投掷:[r] ock,[p] aper还是[s] cissors? [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()

2 个答案:

答案 0 :(得分:0)

我建议创建一个布尔require(lattice) levelplot(Rate_out, at=seq(0, 2, 0.1), xlab = "Grahp_NO", ylab ="10*epsilon", main = "Rate=mean/mean'", col.regions = terrain.colors(100) ) ,在程序开头将其设置为等于was_tied,并将其设置为FalseTrue在每个可能的结果结束时。然后,您可以将打印轮代码放在False语句中。

答案 1 :(得分:0)

所以我认为你有很多方面可以简化这段代码,但是为了快速解决方案,在这一行:

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

将其更改为:

if not previous_round_was_tie:
    print('*'*21 + 'ROUND #'+str(rounds) + '*'*21)
    print()

在您的领带方案为真的所有情况下,添加一行previous_round_was_tie = True。您还必须创建布尔值并将其设置为False循环之外的while