简体   繁体   English

为什么我在索引字典时会收到“字符串索引必须是整数”错误?

[英]Why am I getting a “String indices must be integers” error when I am indicing a dictionary?

I am trying to code a python console game framework.我正在尝试编写 python 控制台游戏框架。 How I have designed it, there is a dictionary called game_state which is updated by a function passed into the loop function.我是如何设计它的,有一个名为 game_state 的字典,它由传递到循环 function 的 function 更新。 After one iteration of the loop, it gives the error "String indices must be integers."在循环的一次迭代之后,它给出了错误“字符串索引必须是整数”。 The stack trace shows that the error is occuring at the line if game_state["running"] == false .堆栈跟踪显示if game_state["running"] == false行发生错误。 When printing out game_state, it seems to be an empty string.打印出 game_state 时,它似乎是一个空字符串。 Why is this happening, and what can I do to fix it?为什么会发生这种情况,我能做些什么来解决它?

def run_loop(calculate, draw, initial_state):
  fd = sys.stdin.fileno()
  old_settings = termios.tcgetattr(fd)
  tty.setraw(sys.stdin)
  game_state = initial_state
  with raw(sys.stdin):
    with nonblocking(sys.stdin):
      try:
        while True:
            try:
                c = sys.stdin.read(1)
                game_state = calculate(c, game_state)
                print_map(draw(game_state))
                if game_state["running"] == False:
                  break
            except IOError:
                print('not ready')
          
            time.sleep(.1)
      finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  
def game(state, press):
  mut_state = state
  if press == "q":
    mut_state["running"] = False
  return mut_state
def display(game_state):
  return [["Yay!"]]
run_loop(game, display, {"running": True})

The answer was right in front of me.答案就在我面前。 I was passing in the input first, meaning that empty input was overwriting the game state.我首先传入了输入,这意味着空输入覆盖了游戏 state。 It should have been calculate(game_state, c) .它应该是calculate(game_state, c)

暂无
暂无

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

相关问题 为什么会出现TypeError:字符串索引必须为整数? - Why am I getting TypeError: string indices must be integers? 为什么我会收到这个 TypeError:字符串索引必须是整数 - why am i getting this TypeError: string indices must be integers 为什么在此行代码中出现错误“字符串索引必须为整数”? - Why am I getting the error 'string indices must be integers' in this line of code? 为什么我会收到有关“字符串索引必须为整数”的错误消息? - Why am I getting this error about “string indices must be integer”? 为什么我看到“TypeError:字符串索引必须是整数”? - Why am I seeing "TypeError: string indices must be integers"? 为什么我会收到类型错误:在调用函数并传递字典时,列表索引必须是整数或切片,而不是 str - Why am I getting a Type error: list indices must be integers or slices, not str, while calling a function and passing a dictionary 我为什么会收到此错误TypeError:列表索引必须是整数或切片,而不是str - Why am I getting this error TypeError: list indices must be integers or slices, not str 为什么我的函数会出现“TypeError:字符串索引必须是整数”? - Why am I getting "TypeError: string indices must be integers" on my function? 我收到python错误TypeError:字符串索引必须是整数,而不是str - I am getting python error TypeError: String indices must be integers, not str 为什么我不断收到“字符串索引必须是整数”错误? - Why do I keep getting a 'string indices must be integers' error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM