Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在for循环中使用变量进行替换_Python_Pygame - Fatal编程技术网

Python:在for循环中使用变量进行替换

Python:在for循环中使用变量进行替换,python,pygame,Python,Pygame,我试图在一个利用pygame的Python程序中压缩重复。我目前有一些行是这样的: if pygame.key.get_pressed()[pygame.K_q]: q.PerformNote() if pygame.key.get_pressed()[pygame.K_w]: w.PerformNote() if pygame.key.get_pressed()[pygame.K_e]: e.PerformNote() if pygame.key.get_pressed()[pygame.K_

我试图在一个利用pygame的Python程序中压缩重复。我目前有一些行是这样的:

if pygame.key.get_pressed()[pygame.K_q]: q.PerformNote()
if pygame.key.get_pressed()[pygame.K_w]: w.PerformNote()
if pygame.key.get_pressed()[pygame.K_e]: e.PerformNote()
if pygame.key.get_pressed()[pygame.K_r]: r.PerformNote()
if pygame.key.get_pressed()[pygame.K_t]: t.PerformNote()
keyList = ['q', 'w', 'e', 'r', 't']
for currentKey in keyList:
    if pygame.key.get_pressed()[pygame.K_currentKey]:
        currentKey.PerformNote()
我想实现这样的目标:

if pygame.key.get_pressed()[pygame.K_q]: q.PerformNote()
if pygame.key.get_pressed()[pygame.K_w]: w.PerformNote()
if pygame.key.get_pressed()[pygame.K_e]: e.PerformNote()
if pygame.key.get_pressed()[pygame.K_r]: r.PerformNote()
if pygame.key.get_pressed()[pygame.K_t]: t.PerformNote()
keyList = ['q', 'w', 'e', 'r', 't']
for currentKey in keyList:
    if pygame.key.get_pressed()[pygame.K_currentKey]:
        currentKey.PerformNote()
由此产生的错误是

AttributeError:“模块”对象没有属性“K_currentKey”


我想我最近可能在BASH上花了太多时间,因为这个结构对我的大脑来说非常合理。我四处搜索,不知道如何正确实现这一点。

将pygame键映射到相应的变量:

keyMap = {
    pygame.K_q: q,
    pygame.K_w: w,
    pygame.K_e: e,
    pygame.K_r: r,
    pygame.K_t: t,
}
然后你可以做:

pressed = pygame.key.get_pressed()
for key, toProc in keyMap.items():
    if pressed[key]:
        toProc.PerformNote()

如果要从名称为的字符串中检索类成员,请尝试:

keyList = ['q', 'w', 'e', 'r', 't']
for currentKey in keyList:
    if pygame.key.get_pressed()[getattr(pygame, 'K_' + currentKey)]:
        currentKey.PerformNote()

请看一看您的键列表应该包含pygame.K,而不是直接的字母名称。此外,在bash中,会有一些东西指示变量的使用。可能是${}或其他什么。可能你需要一个字母的字典地图。我很欣赏这个例子,我觉得整件事看起来很有趣,但我不明白为什么。它就像一个符咒!我很感激在回答我的问题的同时仍然使用我有点破烂的设计。