【问题标题】:Functions, lists, python函数、列表、python
【发布时间】:2014-04-01 18:20:33
【问题描述】:

伙计们。我需要编写一个程序(函数+主程序),它收集单词,将它们放入一个列表中,然后计算单词中的符号,如果符号数多于 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)

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

【问题讨论】:

  • 这段代码有很多错误。我认为您应该先阅读 python 教程。
  • 你的代码还没有输出它应该输出什么?这是一个很好的补充你的问题。

标签: python list count symbols


【解决方案1】:

这段代码会做你想做的事:

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

【讨论】:

    【解决方案2】:

    (假设 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()
    

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 2017-07-25
      • 1970-01-01
      • 2020-10-25
      • 2013-02-19
      • 2021-12-19
      相关资源
      最近更新 更多