Python-在单词列表中搜索字母

Python-在单词列表中搜索字母,python,list,Python,List,我对自动取款机编程是完全陌生的。我搜索了论坛,但没有一个结果适用于我的案例(除非我错了,但相信我,我做的第一件事就是谷歌)。如果你有时间,请帮助我 编写一个python程序,提示用户输入名字列表并将其存储在列表中。程序应显示字母“a”在列表中出现的次数 现在我想我已经得到了完美的代码。但每次程序只计算列表中第一个单词中的“a”的数量 terminate = False position = 0 name_list = [ ] while not terminate: name = st

我对自动取款机编程是完全陌生的。我搜索了论坛,但没有一个结果适用于我的案例(除非我错了,但相信我,我做的第一件事就是谷歌)。如果你有时间,请帮助我

编写一个python程序,提示用户输入名字列表并将其存储在列表中。程序应显示字母“a”在列表中出现的次数

现在我想我已经得到了完美的代码。但每次程序只计算列表中第一个单词中的“a”的数量

terminate = False
position = 0
name_list = [ ]

while not terminate:
    name = str(input('Please enter name: '))
    name_list.append(name)

    response = input('Add more names? y/n: ')
    if response == 'n':
        terminate = True
        print(name_list)
        for t in name_list:
            tally = name_list[position].count('a')
            position = position + 1
        print("The numer of 'a' is: ", tally)
如果有人有时间帮忙,我将不胜感激。

有几点:

  • 您本想使用
    计数
    作为累加器,但实际上并没有添加到累加器中
  • 您不需要
    位置
    索引器,
    for t in name_list
    已经在列表中的所有名称上迭代,在for循环的每次迭代中
    t
    是来自
    name_list
    的名称
  • 修好内环

    terminate = False
    # position = 0 <-- this is not needed anywhere
    name_list = [ ]
    
    while not terminate:
        name = str(input('Please enter name: '))
        name_list.append(name)
    
        response = input('Add more names? y/n: ')
        if response == 'n':
            terminate = True
            print(name_list)
            tally = 0  # initialise tally
            for t in name_list:
                tally += t.count('a')  # increment tally
        print("The numer of 'a' is: ", tally)
    
    terminate=False
    
    #position=0我无法完全理解您的代码。可能是因为有些东西不见了。所以,我假设你已经有了名单。在您显示的代码中,每次计算列表中某个名称中的a数时,您都会覆盖
    tally
    。除了代码中可能存在的其他错误(可能是在创建名称列表时,我会使用while循环),您应该编写

    tally+=name\u list[position]。计数('a')
    等于

    tally=tally+name\u列表[位置].count('a')

    而不是你的


    tally=name\u list[position].count('a')
    我建议您去掉for循环和位置计数器。如果您想要所有
    'a'
    的总数,您必须对所有名称进行总和
    计数

    terminate = False
    name_list = []
    total = 0
    
    while "Adding more names":
        name = str(input('Please enter name: '))
        name_list.append(name)
    
        tally = name.count('a')
        total += tally
        print("The numer of 'a' in current name is: ", tally)
    
        if input('Add more names? y/n: ') == 'n':
            break
    print(name_list)
    print("The numer of 'a' is: ", total)
    

    initilize tally=0,而不是“tally=name_list[position]。count('a')”使用“tally+=name_list[position]。count('a')”如果必须在整个列表中计算“a”,则必须更新计数值

    1。在姓名列表中的项目上循环两次(更准确地说:平方)。2.你忘了把理货的总数加起来。您可以解开两个循环(terminate为false时,如果
    ,则不会输入响应
    ),甚至可以完全删除
    ,只需计算最后一个单词中的字母a(并将其添加到总数中)。而不是第一个。最后!与OP的代码太不一样了,但我会满足于
    print('''.join(name_list).count('a'))
    @usr2564301:我会建议这样做,但我不确定仅仅为了计算字母而连接所有字符串的效率:可能使用
    迭代器.chain
    :dy您也不需要
    terminate
    变量,也不是for循环(因为可以在每个单词后增加总数)
    terminate = False
    position = 0
    name_list = [ ]
    tally = 0
    while not terminate:
        name = str(input('Please enter name: '))
        name_list.append(name)
    
        response = input('Add more names? y/n: ')
        if response == 'n':
            terminate = True
            print(name_list)
            for t in name_list:
                #tally = name_list[position].count('a')
                tally += name_list[position].count('a')
                position = position + 1
            print("The numer of 'a' is: ", tally)