2015-08-13 78 views
-1

嘿,我對Python完全陌生,只使用它2天,而且對編程來說比較新。 我正在嘗試創建一個Rock,Paper,Scissors遊戲。我的問題是,當我運行程序並插入'退出'(應該退出程序)時,程序仍在運行,我不知道爲什麼。有任何想法嗎? 這是我的代碼:無法退出程序 - Python

from random import randint 

#the computer's hand 
def ran_number(): 
    comp_choice = randint(1, 3) 
    if comp_choice == 1: 
    hand = "rock" 
    elif comp_choice == 2: 
    hand = "paper" 
    else: 
    hand = "scissors" 
    return hand 

#game starts 
def play_game(): 
    print("Let's play a game of Rock, Paper, Scissors!") 
    while True: 
    choice = input("Type: 'rock', 'paper', or 'scissors' to play, or 'quit' to end the game.") 
    choice.lower() 
    comp = ran_number() 

    if choice == 'quit': 
     print("\nIt's been a pleasure to play against you! Hope to see you another time.") 
     break 
    elif ((choice == 'rock' and comp == 'paper') or (choice == 'paper' and comp == 'scissors') or (choice == 'scissors' and comp == 'rock')): 
     print("\n{} beats {}! You lose!".format(comp.capitalize(), choice.capitalize())) 
     play_game() 
     continue 
    elif choice == comp: 
     print("\nYou both played {}. It's a tie!".format(choice.capitalize())) 
     play_game() 
     continue 
    else: 
     print("\n{} beats{}! You win!".format(choice.capitalize(), comp.capitalize())) 
     play_game() 
     continue 


play_game() 
+0

'choice.lower()'得到輸入後應該是'choice = choice.lower()'。我不確定這是你的問題;我猜想你是用小寫輸入'quit'。 – Cyphase

+0

@Cyphase:正確,但這裏的主要問題是由於play_game()調用自己,正如Anand&Funky所指出的。 –

回答

0

我想你想說

choice = choice.lower() 

此外,它可能是更安全的做

choice = choice.lower().strip() 

要刪除任何不需要的空白。

1

問題是您每次都會遞歸到play_game()函數中。所以當你給quit輸入時,它退出一個遞歸級別,你必須連續輸入quit的輸入次數,以保證遊戲退出遊戲的次數。

你根本不需要做遞歸,只要將它移除並且它應該沒問題,因爲你也在使用while循環。

此外,另一件事 - choice.lower()是不是在地方,你應該指定它回到choice

此外,elif部分內部不需要continue,因爲在繼續之後您沒有任何語句,所以它會自動繼續循環。

碼 -

from random import randint 

#the computer's hand 
def ran_number(): 
    comp_choice = randint(1, 3) 
    if comp_choice == 1: 
     hand = "rock" 
    elif comp_choice == 2: 
     hand = "paper" 
    else: 
     hand = "scissors" 
    return hand 

#game starts 
def play_game(): 
    print("Let's play a game of Rock, Paper, Scissors!") 
    while True: 
     choice = input("Type: 'rock', 'paper', or 'scissors' to play, or 'quit' to end the game.") 
     choice = choice.lower() 
     comp = ran_number() 

     if choice == 'quit': 
      print("\nIt's been a pleasure to play against you! Hope to see you another time.") 
      break 
     elif ((choice == 'rock' and comp == 'paper') or (choice == 'paper' and comp == 'scissors') or (choice == 'scissors' and comp == 'rock')): 
      print("\n{} beats {}! You lose!".format(comp.capitalize(), choice.capitalize())) 
     elif choice == comp: 
      print("\nYou both played {}. It's a tie!".format(choice.capitalize())) 
     else: 
      print("\n{} beats{}! You win!".format(choice.capitalize(), comp.capitalize())) 

play_game() 
+0

完成,也刪除了無用的繼續 –

+0

再次謝謝。 :) –

0

導入sys模塊並調用exit()函數退出程序。

import sys 

choice = raw_input("Type: 'rock', 'paper', or 'scissors' to play, or 'quit' to end the game: ") 

if choice == 'quit': 
    print("\nIt's been a pleasure to play against you!\n" 
      "Hope to see you another time.") 

sys.exit()