首页 > 解决方案 > 如何根据一个变量的长度使列表中有正确数量的变量

问题描述

我对编码很陌生,正在努力编写刽子手来提高我的技能

我希望代码能够检测单词的长度,然后将该长度放入数组中正确数量的变量中。我真的不知道如何描述它,但是例如,如果单词是 3 个字母长,那么数组中就会存储字母 1、字母 2 和字母 3。类似于 word[0-2] 的东西。

mainword = input("Enter your word")

lengthOfMainword = len(mainword)

number = list(mainword)

correctLetters =[number[0], number[1],number[2],number[3],number[4]#... on until  lengthofmainword is met or something like number[0-lenghthOfMainword]
]

标签: pythonlistarraylistvariable-length

解决方案


你可以试试这个

mainword = input("Enter your word")

lengthOfMainword = len(mainword)

number = list(mainword)
#number contains all the letters in the input
correctLetters = list(set(number))

这里correctLetters将所有字母从用户输入的输入中精确一次


推荐阅读