2016-10-03 76 views
0

這是用python編寫岩石剪刀紙遊戲的代碼。 如果我運行的代碼,它的作品,但是,當它成爲領帶時,它會像這樣輸出。 無論如何,當我得到領帶的結果時,我可以消除打印(圓)嗎? 我想看看它如在示例底部所示岩石剪刀紙程序

********************* ROUND#1 ********* ************

挑選你的投球:[ock,[p] aper或[s] cissors? p 扎!

*********************#1 *********************

挑選你的投球:[ock,[p] aper或[s] cissors? s 電腦扔石頭,你輸了!

你的分數:0 電腦積分:1

********************* ROUND#3 ********* ************

挑選你的投球:[ock,[p] aper或[s] cissors? s 扎!

選擇你的選擇:[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() 
+0

那麼問題是什麼? –

回答

0

我建議創建一個布爾was_tied,將其設置在程序的開始等於False,並在將其設置爲任何TrueFalse每個可能結果的結束。然後,您可以將您的打印輪代碼放入if not was_tied聲明中。

+0

謝謝!它的工作原理 –

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。您還必須創建布爾值並將其設置爲您的while循環外部的False

+0

謝謝! 它在平局上運行,但其他回合如贏和丟失部分沒有輸出一個字符串(回合) –

+0

*********************第一回合* ******************** 接你扔:[ock],[p] aper或[s] cissors? p 扎! 請稍等片刻:[r] ock,[p] aper或[s] cissors? p 電腦扔剪刀,你輸了! 得分:0 計分:1 選擇你的投擲:[r] ock,[p] aper或[s] cissors? –

+0

我修復它謝謝你的幫助 –