简体   繁体   English

在交互式shell和Linux终端(ubuntu 13)中运行python脚本有什么区别?

[英]What's the difference between running python script in interactive shell and in Terminal of Linux(ubuntu 13)?

class Player():
    def __init__(self, char):
            self.char = char
            self.position = 'f'

    def setMove(self):
            while True:
                    print(self.char + ' make a move')
                    self.position = input()
                    if self.position.isdigit():
                            break
    def getMove(self):
            return int(self.position)

    def makeMove(self):
            self.setMove()
            board[self.getMove()].fillCell(self)



class Cell():
    def __init__(self):
            self.filled = False
            self.image = '_'

    def __str__(self):
            return self.image

    def fillCell(self, player):
            if not self.filled:
                    self.image = player.char
                    self.filled = True
            else:
                    player.makeMove()


class Board(list):
    def __init__(self,cells):
            super(list,self).__init__(cells)

    def __str__(self):
            return '\n'.join([chunk for chunk in self._chunks()])

    def _chunks(self):
            chunk_len = 3

            for i in range(0,len(self),chunk_len):
                    yield ' '.join([cell.image for cell in self[i:i+3]])


    def checkRow(self,player):
        chunk_len = 3
        test_list = [player.char for i in range(chunk_len)]

        for idx in range(0,len(self)-1,chunk_len):
            return ([cell.image for cell in self[idx:idx    +chunk_len]] ==    test_list)






board = Board([Cell() for i in range(9)])

if __name__ == '__main__':



pl1 = Player('x')
pl2 = Player('o')



while True:
    print(board)      

    pl1.makeMove()


    print(board)

    pl2.makeMove()

This is my script. 这是我的剧本。 When i ran it in python shell, it counld totally work out. 当我在python shell中运行它时,它完全可以工作了。 However when i tried to do the same thing in Terminal, i just got an error 但是,当我尝试在Terminal中执行相同的操作时,我只是遇到了一个错误

Traceback (most recent call last):
File "tictactoe.py", line 63, in <module>
board = Board([Cell() for i in range(9)])
File "tictactoe.py", line 39, in __init__
super().__init__(cells)
TypeError: super() takes at least 1 argument (0 given) 

then i google it,i add the argument.I ran this script again, it showed a different error. 然后我谷歌它,我添加参数。我再次运行此脚本,它显示了一个不同的错误。

x make a move

Traceback (most recent call last):
File "tictactoe.py", line 77, in <module>
pl1.makeMove()
File "tictactoe.py", line 16, in makeMove
self.setMove()
File "tictactoe.py", line 10, in setMove
if self.position.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'

it didn't surprise me when error occurred, what really surprised me the "board" didn't show up. 当发生错误时,这并不令我感到惊讶,真正让我惊讶的是“标牌”没有出现。 So if can help that would do me great help. 因此,如果可以提供帮助,那会对我有很大帮助。

The difference between running a python script in an interactive shell and in Terminal of Linux is that you can have multiple verions of Python installed. 在交互式外壳程序和Linux终端中运行python脚本之间的区别在于,您可以安装多个版本的Python。 If the wrong one runs, you need to figure out what happened. 如果运行错误,则需要弄清楚发生了什么。

If you run a Python script in an interactive shell, it also uses the libraries corresponding to the Python binary that it gets run in. 如果您在交互式外壳程序中运行Python脚本,则它还会使用与其在其中运行的Python二进制文件相对应的库。

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

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