简体   繁体   English

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

[英]Python - searching for letters in a list of words

I am absolutely new to programming atm. 我绝对不熟悉atm编程。 I have searched the forums but none of the results were applicable to my case (unless I am mistaken, but trust me the first thing I did was to google). 我已经搜索了论坛,但是没有结果适用于我的情况(除非我弄错了,但请相信我做的第一件事就是去Google)。 Please help me if you have time. 如果有时间,请帮助我。

Write a python program that prompts the user to enter a list of first names and stores them in a list. 编写一个python程序,提示用户输入名字列表并将其存储在列表中。 The program should display how many times the letter 'a' appears within the list. 程序应显示字母“ a”出现在列表中的次数。

Now I thought I have got the PERFECT code. 现在我以为我已经有了完善的代码。 But everytime the program only counts the number of "a"s in the first word on the list!? 但是每次程序只计算列表中第一个单词中的“ 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)

If anyone has time to help I would appreciate it. 如果有人有时间帮忙,我将不胜感激。

A couple of points: 要点:

  1. you meant to use tally as an accumulator but are actually not adding to it. 您本来打算将tally用作累加器,但实际上并没有增加累加器。
  2. You don't need the position indexer, for t in name_list already iterates over all names in the list, at each iteration of the for loop t is a name from name_list . 您不需要position索引器, for t in name_list已经在列表中的所有名称上进行了迭代,在for循环的每次迭代中tname_list的名称。

fix the inner loop 修复内循环

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)

Your code is not fully understandable to me. 您的代码对我来说不是完全可以理解的。 Probably, because some stuff is missing. 可能是因为缺少某些东西。 So, I am assuming, you already have the list of names. 因此,我假设您已经有了名称列表。 In the code you presented, you are overwriting tally each time you count the number of a in a name in the list. 在您提供的代码中,每当您计算列表中名称中的a数时,您都将覆盖tally Besides other possible bugs in your code (maybe when creating the list of names - I'd use a while-loop for this), you should write 除了代码中可能存在的其他错误(也许在创建名称列表时-我将为此使用while循环),还应编写

tally += name_list[position].count('a') which equals tally += name_list[position].count('a')等于

tally = tally + name_list[position].count('a')

instead of your 而不是你的

tally = name_list[position].count('a')

I would suggest you get rid of the for loop, and position counter. 我建议您摆脱for循环和位置计数器。 If you want the total of all 'a' , you have to sum tally for all the names : 如果你想将总的'a' ,你必须要总结tally所有的名字:

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)
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)

initilize tally=0 and insted of "tally = name_list[position].count('a')" use "tally += name_list[position].count('a')" if you have to count "a" in entire list you have to update the tally value 初始化tally = 0并插入“ tally = name_list [position] .count('a')”如果需要在整个列表中计算“ a”,请使用“ tally + = name_list [position] .count('a')”您必须更新计数值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM