TypeError:关键字必须是字符串

时间:2019-03-14 20:33:25

标签: python python-3.x python-2.7

我正在尝试转换此程序

https://github.com/adambeagle/JeoparPy

从2.7到3.7。我已经用2.7运行了程序,并且可以正常工作。

我已经对xrange-> range和basestring-> str进行了修改。我还更改了导入路径。

这是执行start.py时出现的错误:

Traceback (most recent call last):
  File "start.py", line 64, in <module>
    main(*flags)
  File "D:\Users\Michael\IdeaProjects\JeoparPy\jeoparpy\main.py", line 55, in main
    uicontroller = Controller(screen, gameData, FPS_LIMIT)
  File "D:\Users\Michael\IdeaProjects\JeoparPy\jeoparpy\ui\controller.py", line 57, in __init__
    self.audioplayer = JeopAudioPlayer()
  File "D:\Users\Michael\IdeaProjects\JeoparPy\jeoparpy\ui\audioplayer.py", line 111, in __init__
    super().__init__(dict(SOUNDS, **reads))
TypeError: keywords must be strings

错误“关键字必须是字符串”是指此类:

我不明白哪个变量不是字符串。 “ pos”似乎是代表位置的局部变量。

class JeopAudioPlayer(AudioPlayer):
    """An AudioPlayer with JeoparPy sounds initialized."""
    def __init__(self):
        reads = {}
        for pos, path in CLUE_READS.items():
            key = pos + ('cr', )
            reads[key] = path

        super().__init__(dict(SOUNDS, **reads))

这是来自resmaps的SOUNDS字典:

SOUNDS = {(0, 4) : _sndPath + 'sample_music.ogg',
          (1, 4) : _sndPath + 'sample_music.ogg',
          (2, 4) : _sndPath + 'sample_music.ogg',
          (3, 4) : _sndPath + 'sample_music.ogg',
          (4, 4) : _sndPath + 'sample_music.ogg',
          }

CLUE_READS词典非常相似:

CLUE_READS = {(0, 2) : _cluesPath + 'sample_read.ogg',
              (1, 2) : _cluesPath + 'sample_read.ogg',
              (2, 2) : _cluesPath + 'sample_read.ogg',
              (3, 2) : _cluesPath + 'sample_read.ogg',
              (4, 2) : _cluesPath + 'sample_read.ogg',
              }

这是定义路径的方式:

from jeoparpy.constants import ROOT_PATH

_fontPath = path.join(ROOT_PATH, 'res', 'fonts', '')
_imgPath = path.join(ROOT_PATH, 'res', 'images', '')
_sndPath = path.join(ROOT_PATH, 'res', 'sounds', '')
_cluesPath = path.join(ROOT_PATH, 'res', 'sounds', 'clues', '')

以防万一,这里是ROOT_PATH:

ROOT_PATH = path.abspath(path.join(path.dirname(__file__), pardir))

1 个答案:

答案 0 :(得分:1)

SOUNDSreads都已经是字典。将它们作为关键字参数传递给dict构造函数毫无意义;只需更新另一个即可。由于reads完全是在方法中创建的,因此可以安全地直接使用SOUNDS的内容对其进行更新。

reads.update(SOUNDS)
super().__init__(reads)