2017-08-14 81 views
-2

任何人都可以幫助我理解爲什麼我非常簡單的岩石剪刀代碼卡在第18行末尾並退出? 我已經測試過每個部分單獨和它的工作,它可能不是最漂亮的代碼,但它似乎做的工作,但在它的最新itteration它只是退出第18行,退出代碼0,所以沒有錯誤,一點兒也不說什麼是錯的,它只是doesn't執行下一行,就像看到一隻破或該線路上的退出,但有isn't:爲什麼我的python代碼沒有完全運行

import random 

def startgame(): 
    print("Please choose rock - r, paper - p or scissors - s:") 
    pchoice = input(str()) 
    if(pchoice.lower in ["r","rock"]): 
     pchoice = "0" 
    elif(pchoice.lower in ["s","scissors"]): 
     pchoice = "1" 
    elif(pchoice.lower in ["p","paper"]): 
     pchoice = "2" 
    cchoice = (str(random.randint(0,2))) 
    if(cchoice == "0"): 
     print("Computer has chosen: Rock \n") 
    elif(cchoice == "1"): 
     print("Computer has chosen: Scissors \n") 
    elif(cchoice == "2"): 
     print("Computer has chosen: Paper \n") 
#runs perfect up to here, then stops without continuing 
    battle = str(pchoice + cchoice) 
    if(battle == "00" and "11" and "22"): 
     print("Draw! \n") 
     playagain() 
    elif(battle == "02" and "10" and "21"): 
     if(battle == "02"): 
      print("You Lose! \nRock is wrapped by paper! \n") 
     elif(battle == "10"): 
      print("You Lose! \nScissors are blunted by rock! \n") 
     elif(battle == "21"): 
      print("You Lose! \nPaper is cut by scissors! \n") 
      playagain() 
    elif(battle == "01" and "12" and "20"): 
     if(battle == "01"): 
      print("You Win! \nRock blunts scissors! \n") 
     elif(battle == "12"): 
      print("You Win! \nScissors cut paper! \n") 
     elif(battle == "20"): 
      print("You Win! \nPaper wraps rock! \n") 
      playagain() 

def main(): 
    print("\nWelcome to Simon´s Rock, Paper, Scissors! \n \n") 
    startgame() 

def playagain(): 
     again = input("Would you like to play again? y/n \n \n") 
     if(again == "y"): 
      startgame() 
     elif(again == "n"): 
      print("Thank you for playing") 
      exit() 
     else: 
      print("Please choose a valid option...") 
     playagain() 

main() 
+0

嘗試打印戰鬥值,並將其與連續if語句中的內容進行比較。 –

+0

你有沒有重複這個問題的條目? – Miket25

+3

提供的代碼有很多問題。有些已經在答案中解釋過,有些則不是。例如,無論你有'.lower'還是'.upper',都應該是'lower()'和'upper()'。否則,這些「if」條件永遠不會是「真」。 – DeepSpace

回答

0

在像 if(battle == "00" and "11" and "22"): 使用in操作線 if(battle in ["00", "11", "22"]):

playagain()沒有被調用,因爲沒有條件是真的。

0

的錯誤是在這:

if(battle == "00" and "11" and "22"): 

這將評估在所有情況下False00,您需要將其更改爲:

if battle == "00" or battle == "11" or battle == "22": 

而且也是您使用and

您的發言被解讀爲以下其他兩個語句:由於您使用

True/False 1- if battle == "00" 
True  2- and "11" #<-- here it checks if the string is True which means string is not empty 
True  3- and "22" is True #<-- here too 

所以,你的語句將只有當所有陳述True工作and要求聲明的所有部分爲True。第二和第三部分總是True因此它的檢查,如果選擇的是"00"

你需要的是這樣的:

1- if battle == "00" True/False 
2- or battle == "11" True/False 
3- or battle == "22" True/False 

而你只需要一個部分是True這裏,因爲運行該語句的or

相關問題