2017-04-30 112 views
-3

我目前正在爲編程課程介紹完成一個項目,並且在使用此菜單驅動的遊戲時遇到了麻煩。它給我一個無效的語法錯誤。我已經包含了代碼,非常感謝任何幫助!非常感謝。 道格Python中的岩石紙剪刀遊戲

import random 
p1 = 0 
p2 = 0 
cpu = 0 
tie = 0 
def main(): 
    menuSelect = "" 
    print("Welcome to Rock, Paper, Scissors!\n") 

# main menu 
    print("Main Menu") 
    print("1. See the rules") 
    print("2. Play against the computer") 
    print("3. Play a two player game") 
    print("4. Quit\n") 

    menuSelect = int(input("Please make choice 1-4:\t")) 

    while menuSelect < 1 or menuSelect > 4: 
     print("The selection provided is invalid.\n") 
     menuSelect = int(input("Please select one of the four options:\t")) 

    if menuSelect == 1: 
     rules() 
    elif menuSelect == 2: 
     onePlayer() 
    elif menuSelect == 3: 
     twoPlayer() 
    elif menuSelect == 4: 
     endGame() 

# display the rules to the user 
def rules(): 

    print("\n\nRules") 
    print("________________________") 
    print("Paper Covers Rock") 
    print("Rock Smashes Scissors") 
    print("Scissors Cut Paper\n") 
    main() 


# one player mode 
def onePlayer(): 
    p1 = 0 
    p2 = 0 
    cpu = 0 
    tie = 0 
    again = "" 
    player = False 

    print("\nPlayer VS Computer") 

    while player == False: 
     print("Select a Weapon!") 
     print("1. Rock") 
     print("2. Paper") 
     print("3. Scissors") 
     print("4. Return to Main Menu") 
     player = int(input("\nWhat is your weapon of choice(1-4):\t")) 

     #computer = WEAPON[randint(0,2)] 
     #temporary 
     computer = random.randint(1,3) 

     if player == computer: 
      print("It's a tie!\n") 
      tie = tie + 1 
     elif player == 1: 
      if computer == 2: 
       print("Paper covers rock! You lose!\n") 
       cpu = cpu + 1 
      else: 
       print("Rock smashes scissors. You win!\n") 
       p1 = p1 + 1 
     elif player == 2: 
      if computer == 3: 
       print("Scissors cut paper! You lose!\n") 
       cpu = cpu + 1 
      else: 
       print("Paper covers rock. You win!\n") 
       p1 = p1 + 1 
     elif player == 3: 
      if computer == 1: 
       print("Rock smashes scissors! You lose!\n") 
       cpu = cpu + 1 
      else: 
       print("Scissors cut paper. You win!\n") 
       p1 = p1 + 1 
     else: 
      print("invalid input") 

     print("Scores for this session:\n") 
     print("Ties:\t", tie) 
     print("Computer:\t", cpu) 
     print("Player 1:\t", p1) 

     again = input("Would you like to play again? Yes or No\n") 
     again = again.lower() 

     if again=="yes" or again=="y": 
      player = False 
     else: 
      player = True 
      main() 


def twoPlayer(): 
    p12 = 0 
    p2 = 0 
    tie2 = 0 
    again = "" 
    player1 = False 
    player2 = 0 

    print("\nPlayer VS Computer") 

    while player1 == False: 
     print("Select a Weapon!") 
     print("1. Rock") 
     print("2. Paper") 
     print("3. Scissors") 
     print("4. Return to Main Menu") 
     player1 = int(input("\nPlayer 1 what is your weapon of choice(1-4):\t")) 
     player2 = int(input("\n\nPlayer 2 what is your weapon of choice(1-4):\t") 

     if player1 == player2: 
      print("It's a tie!\n") 
      tie2 = tie2 + 1 
     elif player1 == 1: 
      if player2 == 2: 
       print("Paper covers rock! You lose!\n") 
       p2 = p2 + 1 
      else: 
       print("Rock smashes scissors. You win!\n") 
       p12 = p12 + 1 
     elif player1 == 2: 
      if player2 == 3: 
       print("Scissors cut paper! You lose!\n") 
       p2 = p2 + 1 
      else: 
       print("Paper covers rock. You win!\n") 
       p12 = p12 + 1 
     elif player1 == 3: 
      if player2 == 1: 
       print("Rock smashes scissors! You lose!\n") 
       p2 = p2 + 1 
      else: 
       print("Scissors cut paper. You win!\n") 
       p12 = p12 + 1 
     else: 
      print("invalid input") 

     print("Scores for this session:\n") 
     print("Ties:\t", tie2) 
     print("Player 1:\t", p12) 
     print("Player 2:\t", p2) 

     again = input("Would you like to play again? Yes or No\n") 
     again = again.lower() 

     if again=="yes" or again=="y": 
      player1 = False 
     else: 
      player1 = True 
      main() 


main() 
+0

當然追溯查明錯誤位置(或下落)你至少應該補充一點,做你的。問題,如果你不能自己弄清楚。 – DeepSpace

回答

0

在這部分代碼,在你結束 「而PLAYER1 ==假:」 循環:

player2 = INT(輸入(「\ n \ nPlayer 2你是什麼選擇的武器(1-4):\ t「的)

只需插入一個又一個括號,它應該運行