嘿,我對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()
'choice.lower()'得到輸入後應該是'choice = choice.lower()'。我不確定這是你的問題;我猜想你是用小寫輸入'quit'。 – Cyphase
@Cyphase:正確,但這裏的主要問題是由於play_game()調用自己,正如Anand&Funky所指出的。 –