变量未在Python 3.5.1中定义

时间:2016-03-06 17:47:37

标签: python-3.x

我是Python的新编码器,我想知道如何解决这个问题。每当我在我的代码中输入正确的输入时,它就会发出错误信息,就像这样。

代码

... RECT{ static_cast<LONG>(Position.left + 3), ... and next fileds too

输出

import random,math

class OcenTreasure:
    def __init__(self):
        self.board=[]
        self.chests=[]
        self.treasure=3
        self.sonar=20
        self.drop=[]

    def get_board(self):
        colume=['~']*60
        for y in range(0,15):
            self.board.append(colume)
        return self.board

    def drawBoard(self):
        row=0
        print('             1         2         3         4         5 ',\
              '   012345678901234567890123456789012345678901234567890123456789',\
              sep='\n')
        while row<15:
            if row<10:
                line=''.join(self.board[row])
                print(' '+str(row),line,str(row))
            if row>=10:
                line=''.join(self.board[row])        
                print(str(row),line,str(row))
            row+=1
        print('             1         2         3         4         5 ',\
              '   012345678901234567890123456789012345678901234567890123456789',\
              sep='\n')  

    def getChests(self):
        cont=True
        while cont==True:
            for i in range(0,3):
                a=random.randint(0,14)
                b=random.randint(0,59)
                aList=[b,a]
                if aList in self.chests:
                    aList=[]
                else:
                    self.chests.append(aList)
                    cont=False

        return self.chests


    def dropSonar(self,choice):
        if choice in self.drop:
            self.sonar-=1
            return (print('You already dropped a sonar there. You lost another sonar device'))
        else:
            self.drop.append(choice)
        pos=choice.split()
        x=int(pos[0])
        y=int(pos[1])
        for item in self.chests:
            xgap=math.fabs(x-item[0])
            ygap=math.fabs(y-item[1])
            if ygap>5 and xgap>9:
                self.board[y][x]='O'
            else:
                if xgap==0 and ygap==0:
                    self.board[y][x]='X'
                    self.chests.remove(item)
                    self.treasure-=1
                if xgap<2*ygap:
                    self.board[y][x]=str(int(xgap))
                if xgap>=2*ygap:
                    if ygap==1:
                        self.board[y][x]='a'
                    if ygap==2:
                        self.board[y][x]='b'
                    if ygap==3:
                        self.board[y][x]='c'
                    if ygap==4:
                        self.board[y][x]='d'                    
                    if ygap==5:
                        self.board[y][x]='e'
        self.sonar-=1

gameOver=False 
t=OcenTreasure()
t.get_board()
a=t.getChests()
print(a)
while gameOver==False:
    t.drawBoard()    
    print(t.board)
    print('Where do you want to drop your sonar?')
    if t.sonar>0:
        choice=input('Enter coordinates x y (x in [0..59] and y in [0..14]) (or Q to quit and H for help): ').lower()
        if choice=='q':
            print('The chests were in: '+str(t.chests))
            print('Thank you for playing Ocean Treasures')
            gameOver=True
        else:
            t.dropSonar(choice)
            if t.treasure==0:
                print('Well done! You found all the 3 treasure Chests using '+\
                      str(t.sonar)+' out of 20 sonar devices.')
                gameOver=True
            else:
                print('You have '+str(t.sonar)+\
                      ' sonar devices available. Treasures found: '\
                      +str(3-t.treasure)+'. Still to be found: '+\
                      str(t.treasure)+'.')
    else:
        print('You lost all your 20 sonar devises.')
        print('The remaining chests were in: '+str(t.chests))
        gameOver=True

这是版本3.5.1

3 个答案:

答案 0 :(得分:2)

您需要使用值yes声明变量'yes',或者将变量yesnoA与字符串'yes'进行比较。也许是这样的:

if yesnoA.lower() == 'yes': # using lower(), so that user's input is case insensitive
    # do the rest of your work

之后您的代码还有一些问题。我会告诉你一个线索。 input始终将用户的输入作为字符串返回。因此,如果您需要来自用户的整数,则必须使用int(your_int_as_string)将用户输入转换为整数,如下所示:

turnAA = int(input('Your First Move'))
# turnAA is now an integer, provided the user entered valid integer value

你对这个问题的看法是:

  • 看看Traceback。它清楚地说明错误在哪一行,以及错误是什么。在您的情况下,它是NameError
  • 查看NameError
  • 的文档
  • 研究this tutorial。它将帮助您适应常见的一些基本错误。

答案 1 :(得分:1)

您尚未定义变量yes。你应该做点什么:

yes = "Yes"

代码的开头

答案 2 :(得分:0)

您正在尝试将yesnoA与名为yes的变量进行比较,该变量确实未定义,而不是字符串文字'yes'(请注意引号!)。添加引号,您应该没问题:

if yesnoA == 'yes':
    # Here --^---^