Python 从用户输入获取列表中每个元素的长度

Python 从用户输入获取列表中每个元素的长度,python,Python,我正在尝试实现这个代码,我有一个字符串列表,secret\u word=[“the”,“blue”,“house”] 当用户选择单词时,user\u pick=input(“选择秘密单词:”),如果它在secret\u word列表中,我将附加到新列表中,right\u words=[]。如果他们想知道这个单词的线索,它会打印出这个单词的字母数。问题是,我无法获得特定字符串的字母数。例如:如果用户正确地选择了“”,但询问第二个单词的线索,我如何给出第二个单词的字母数。在本例中,“蓝色”。同样,如

我正在尝试实现这个代码,我有一个字符串列表,
secret\u word=[“the”,“blue”,“house”]
当用户选择单词时,
user\u pick=input(“选择秘密单词:”)
,如果它在
secret\u word
列表中,我将附加到新列表中,
right\u words=[]
。如果他们想知道这个单词的线索,它会打印出这个单词的字母数。问题是,我无法获得特定字符串的字母数。例如:如果用户正确地选择了“”,但询问第二个单词的线索,我如何给出第二个单词的字母数。在本例中,“蓝色”。同样,如果用户正确地选择了
“蓝色”
,并要求提供第三条线索,则线索应打印为
“房子”
5

我得到1作为线索,这是在选择
“the”
之后列表的长度,但是我需要4个字母作为
“blue”
(如果用户已经正确选择了“the”),如果用户正确选择了“blue”,那么5个字母作为线索。
我真的不知道如何才能使这个代码工作!请帮忙:)如果有人有任何想法吗?

请保留一个计数器
c
,它可以保存到目前为止正确猜测的数量。 然后打印出
len(秘密单词[c])

请注意,
如果用户选择secret\u word
可能在语义上不正确,因为它让您的用户以任意顺序猜测
secret\u word
的元素。

请尝试以下操作:

secret_words = ["the", "blue", "horse"]  # Yur secret words list


# Creating function that takes one parameter: word_index
def get_length_of_word(word_index):
    global secret_words  # Globalizing the variable
    # If the following statement is difficult to understand, let me break it down for you
    return len(secret_words[word_index])

    '''
    Lets assume word_index = 1
    The statement: secret_words[word_index] will use the 1st item in our list (the list starts from 0)
    In our case, the first item is 'blue' (not 'the', that is acually the 0th item)
    The len() statement then takes the length of the string
    Since it returns the value, you can assign it to a variable and use it later on
    '''


print(get_length_of_word(1)) # Printing the length of the first word in our list ('blue').

# You could assign to a variable, with something like: 
# length_of_second_word = get_length_of_word(1)
试试这个:

import random
secret_word = ["the","blue","house"]
right_words = []

while len(secret_word) != 0:  #a while loop so the user can have a few tries, the loop stops when the user guessed all of the words correctly
    user_pick = input("Pick the secret word: ")
    if user_pick in secret_word:
        right_words.append(user_pick)
        secret_word.remove(user_pick) #avoid a user picking the same word twice
    else:
        clue_input = input("Do you want a clue? y/n \n") #ask the user if they want a clue
        if clue_input == 'y': #only runs in the case of 'y', so the user can just press enter for no
            clue_word = random.choice(secret_word) #pick a random word from the secret words
            print("Number of letters in a word: " + str(len(clue_word))) #print the length of that word to the user

另一个版本。关键是使用一个你现在知道的计数器…然后迭代它。计数是按索引进行的,所以

while counter <=2

请注意,这只是为了得到一个单词的长度,而不是整个程序:)谢谢你的回答。哦,它成功了!!!非常感谢。我一整天都在努力。请你解释一下好吗?它是如何工作的?索引@蒂姆盖布
while counter <=2
secret_word = ["the","blue","house"]
right_words = []

counter = 0

while counter <=2:
    user_pick = input("Pick the secret word: ")
    if user_pick == secret_word[counter]:
          right_words.append(user_pick)


    if user_pick != secret_word[counter]:
          print("clue", "the number of letters in this word:", len(secret_word[counter]))
          counter -= 1

    if len(right_words) == len(secret_word):
        break
    counter += 1

print(right_words)


Pick the secret word: the
Pick the secret word: wrong
clue the number of letters in this word: 4
Pick the secret word: blue
Pick the secret word: wrong
clue the number of letters in this word: 5
Pick the secret word: wrong
clue the number of letters in this word: 5
Pick the secret word: house
['the', 'blue', 'house']