【问题标题】:Trying to make a list that is ['_', '_', '_'] with the length depending on a for loop尝试创建一个长度取决于 for 循环的 ['_', '_', '_'] 列表
【发布时间】:2022-06-10 23:37:41
【问题描述】:

我正在尝试列出以后可以单独填写的“空白”列表。列表的长度取决于一个值 (currentWord),这就是我无法对其进行硬编码的原因。

iter8 = len(currentWord)
for i in currentWord:
    answerListNEW.append(' ')
    #
for i in currentWord:
    answerListNEW[i] = '_'

我得到索引必须是整数而不是 str 的错误。程序有没有办法接受“空白”? 为简单起见,假设 currentWord = 'alpha' 这样它就很简单,只有 5 次迭代。

【问题讨论】:

  • 欢迎来到 Stack Overflow。在代码中显示 for i in currentWord: 的地方,您希望 i 在每次循环中都有什么值?为什么?您是否尝试检查这些值是什么?您是否尝试过遵循 Python 教程来理解这些基础知识?

标签: python list


猜你喜欢
  • 1970-01-01
  • 2021-09-13
  • 2021-12-19
  • 2019-05-27
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
相关资源
最近更新 更多