繁体   English   中英

Python2.7:这个程序有什么问题? 它运行但奇怪的回溯错误

[英]Python2.7: What is wrong with this program? It runs but the weird traceback error

程序运行良好。 但是出现这个错误(截图)。 它只是空白。 它的Python 2.7 我也将 Python 添加到环境变量中,但 shell 上也没有显示任何内容。

石头纸和剪刀代码

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 and 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_int("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."
            computer_score += 1


        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 your next time!"

        def scores():
            global player_score,computer_score
            print "High Scores"
            print "Player:" , player_score
            print "Computer:", computer_score

        if _name_ == '_main_':
          start()

错误:

回溯(最近一次调用):文件“C:/Users/Sarthak/Desktop/RPS.py”,第 80 行,在 if name == ' main ': NameError: name ' name ' 未定义

您应该修复缩进和所有其他错误:

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 and 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 #this is not how you could get int input in Python
        player = raw_int("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." #note the indentation here

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."
            computer_score += 1


def play_again(): #again, indentation
    #and this is not how you could get string input in Python
    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 your next time!"

def scores(): #note the indentation here
    global player_score,computer_score
    print "High Scores"
    print "Player:" , player_score
    print "Computer:", computer_score

if __name__ == '__main__': #note the underscores here
  start()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM