函数,列表,python

时间:2014-02-28 16:20:24

标签: python list count symbols

人。我需要编写一个程序(函数+主程序),它收集单词,将它们放入一个列表,然后计算单词中的符号,如果有多个符号而不是数字N,则将其放入list2。然后我需要打印出第二个列表。这是我到目前为止所得到的:


def WordsInLists(word, symbols, number):
    list1 = [word]
    list2 = [len.word > n]
    return(list2)

list1 = []
list2 = []
howmany = int(input("How many words will you write?"))
n = int(input("What will the n number be?"))
for i in range(0, howmany, 1):
        word = (input("Write the word "))
        list1 = list1 + [word]
        if len.word > n:
            list2 = list2 + [word]
        result = WordsInLists(list2) 
        print(result)

我接下来应该做什么或者我应该改变什么?

2 个答案:

答案 0 :(得分:0)

(这假设是Python 3.x):

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:  # not an int
            pass

def main():
    wordlen = get_int("How many words to enter? ")

    words = input("Please enter the words, separated by spaces:\n").split()
    list1, list2 = words[:wordlen], words[wordlen:]

    print("Leftover words: {}".format(", ".join(list2)))

if __name__=="__main__":
    main()

答案 1 :(得分:0)

此代码将执行您想要的操作:

list1 = []
list2 = []
howmany = int(input("How many words will you write?"))
n = int(input("What will the n number be?"))
for i in range(0, howmany, 1):
        word = raw_input("Write the word ")
        list1 = list1 + [word]
        if len(word) > n:
            list2 = list2 + [word]
print list2