簡體   English   中英

如何根據一個變量的長度使列表中有正確數量的變量

[英]How to make list have correct number of variables inside them depending on length of one variable

我對編碼很陌生,正在努力編寫劊子手來提高我的技能

我希望代碼能夠檢測單詞的長度,然后將該長度放入數組中正確數量的變量中。 我真的不知道如何描述它,但是例如,如果單詞是 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]
]

你可以試試這個

mainword = input("Enter your word")

lengthOfMainword = len(mainword)

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

在這里, correctLetters將從用戶輸入的輸入中獲得所有字母

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM