【问题标题】:What's the difference between running python script in interactive shell and in Terminal of Linux(ubuntu 13)?在交互式 shell 和 Linux(ubuntu 13)终端中运行 python 脚本有什么区别?
【发布时间】:2014-03-12 09:51:04
【问题描述】:
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()

这是我的脚本。当我在 python shell 中运行它时,它完全可以解决。但是,当我尝试在终端中做同样的事情时,我得到了一个错误

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) 

然后我谷歌它,我添加了参数。我再次运行这个脚本,它显示了一个不同的错误。

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'

发生错误时并没有让我感到惊讶,真正让我感到惊讶的是“板”没有出现。所以如果能帮上忙,那对我有很大帮助。

【问题讨论】:

  • 看起来您正在 Python 2 中运行 Python 3 脚本。super()input() 在那里的工作方式不同。

标签: python linux shell terminal interpreter


【解决方案1】:

在交互式 shell 和 Linux 终端中运行 python 脚本的区别在于您可以安装多个版本的 Python。如果跑错了,你需要弄清楚发生了什么。

如果您在交互式 shell 中运行 Python 脚本,它还会使用与运行它的 Python 二进制文件相对应的库。

【讨论】:

  • 那么如何更改在我的终端中运行的 python 版本。
猜你喜欢
  • 2014-05-06
  • 2013-08-13
  • 2018-01-23
  • 2020-01-15
  • 2021-07-11
  • 2017-02-13
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多