如何在python中找到未知列表中的项目?

时间:2017-11-10 09:51:34

标签: python

我有一个str单个字符列表,在真实模型中我不知道“word_to_guess”。如果用户的输入(char_guess)是列表中的项目,我需要在列表中找到该字符的位置?

word_to_guess = 'cat' #usually random from user input list but 'cat' while testing
listed_word = list(word_to_guess)

char_guess = input("Enter a character you would like to guess ")

测试用例:

word_to_guess = "cat" #using as an example although its unknown due to previous code at the bottom for reference

listed_word = list(word_to_guess) #which is ['c', 'a', 't']

char_guess = input("Enter a character you would like to guess ")
#user inputs 'a' and that is what char_guess is set to

如果角色在列表中(它是),我将如何找到该角色的位置?我如何找到['c','a','t']中的'a'的位置

本节前面的代码

import random
word_choice_list = [ ]
more_word = "no"
word = input("Enter a word you would like to try and guess ")
word_choice_list.append(word)
print(" * this a yes or no question * ")
more_word = input("Would you like to continue inputting words to guess? ")
if more_word == "yes":
    print("Please input a new word")
    more_word = "yes"
elif more_word == "no":
    print("List of possible words is complete")
    more_word = "no"
else:
    print('Incorrect answer, please try again')
    more_word = "yes"
while more_word == "yes":
    word_2 = input("Enter a word you would like to try and guess ")
    if word == word_2:
        print("You have already inputted this word")
    else:
        word_choice_list.append(word_2)
    print(" * this a yes or no question * ")
    more_word = input("Would you like to continue inputting words to guess? ")
    if more_word == "yes":
        print("Ple input a new word")
        more_word = "yes"
    elif more_word == "no":
        print("List of possible words is complete")
        more_word = "no"
    else:
        print('Incorrect answer, please try again')
        more_word = "yes"

sr = random.SystemRandom()
word_to_guess = sr.choice(word_choice_list)

1 个答案:

答案 0 :(得分:-1)

确实

$row = $xml->children()->children()->associations->cart_rows->addChild('cart_row');

$row->addChild('id_product', $cart_itm["code"]);
$row->addChild('quantity', $cart_itm["qtyOrSomething"]);