因此,我正在编写一个程序,它将接收用户输入,然后将用户输入与一个设置列表进行比较,然后告诉我给定列表中有多少来自用户输入的单词
例如:
list = ['I','like','apples'] # set list
user_in = input('Say a phrase:')
# the user types: I eat apples.
#
# then the code will count and total the similar words
# in the list from the user input.
我已经接近这一点,我知道我可能必须将用户输入转换为列表本身。只是需要帮助比较和计算匹配的单词
多谢各位
那么,您可以使用user_in.split(“”)分割用户输入。 然后将user_in_列表中的每个单词与检查列表中的一个单词进行比较,在这种情况下增加计数器:
试着这样做:
similarWords=0 #initialize a counter for word in user_in.split(): if word in list: #check and compare if word is in set list similarWords+=1 #increase counter by 1 every time a word matches
相关问题 更多 >
编程相关推荐