【问题标题】:Python rock, paper, scissorsPython 石头、纸、剪刀
【发布时间】:2013-06-21 15:33:00
【问题描述】:

我正在尝试学习 python 并从这个开始,当我尝试运行它时,我不断收到语法错误。光标跳到 def start 部分的“关闭”末尾。我不确定语法错误来自哪里,因为我语音标记了所有打印

#! python3

# J Presents: Rock, paper, Scissors: The Video Game

import random
import time

rock = 1
paper = 2
scissors = 3

names = { rock: "Rock", paper: "Paper", scissors: "Scissors" }
rules = {rock: scissors, paper: rock, scissors: paper}

Player_score = 0
computer_score = 0

def start ():
    print "Let's play a game of Rock, Paper, Scissors."
    while game ():
        pass
    scores()

def game ():
    player = move ()
    computer = random.randint(1, 3)
    result(player, computer)
    return play_again()

def move():
    while True:
        print
        player = raw_input("Rock = 1\nPaper = 2\nScissors = 3\nMake a Move: ")
        try:
            player = int(player)
            if player in (1,2,3):
                return player
            except ValueError:
                pass
            Print "Oops! I didn't understand that. Please enter 1, 2 or 3."

def result (player, computer):
    print "1..."
    time.sleep(1)
    print "2..."
    time.sleep(1)
    print "3!"
    time.sleep (0.5)
    print "Computer threw {0}!".format(names[computer])
    global player_score, computer_score
    if player == computer:
        print "Tie Game."
    else:
        if rules[player} == computer:
        print "Your victory has been assured."
        player_score +=1
    else:
        print "The computer laughs as you realise you have been defeated."

def play_again():
    answer = raw_input("Would you like to play again? y/n: ")
    if answer in ("y", "Y", "yes", "Yes", "Of course!"):
        return answer
    else:
        print "Thank you very much for playing our game. See you next time!"

def scores():
    global player_score, computer_score
    print " HIGH SCORES"
    print "Player: ", player_score
    print "Computer: ", computer_score

if __name__ == '__main__':
    start()

【问题讨论】:

标签: python syntax syntax-error


【解决方案1】:

1) 你这里有一个缩进错误:

try:
            player = int(player)
            if player in (1,2,3):
                return player
except ValueError: #Try Except Block Statement
            pass
            Print "Oops! I didn't understand that. Please enter 1, 2 or 3."

2) 还有:

if rules[player} == computer: #Error in this line

应该是:

if rules[player] == computer:

3) if else 中的缩进错误。

if player == computer:
    print "Tie Game."
else:
    if rules[player} == computer:
    print "Your victory has been assured."
    player_score +=1
else: #Incorrect indentation
    print "The computer laughs as you realise you have been defeated."

这应该是:

    if rules[player} == computer:
        print "Your victory has been assured."
        player_score +=1
    else:
        print "The computer laughs as you realise you have been defeated."

【讨论】:

    【解决方案2】:
    if rules[player} == computer:
    

    大括号应该是括号。

    【讨论】:

      【解决方案3】:
      if rules[player} == computer:
      print "Your victory has been assured."
      player_score +=1
      

      第一个错误:rules[player} => rules[player] 第二个错误,你需要识别if之后的行。

      else:
          if rules[player} == computer:
              print "Your victory has been assured."
              player_score +=1
          else:
              print "The computer laughs as you realise you have been defeated."
      

      第三个错误:一个 if 不能有 2 个 else

      【讨论】:

        猜你喜欢
        • 2020-06-20
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 2020-05-22
        • 1970-01-01
        相关资源
        最近更新 更多