如何获取每个单词的字母总数

huangapple go评论70阅读模式
英文:

how to i get the total number of letters for each word

问题

我尝试创建一个程序,用于统计输入的每个单词中字母的数量。我还希望程序能够打印出具有最多字母数量的单词以及与之对应的字母数量。我不知道如何统计每个单词中的字母数量。

这是我目前的代码。如何获取字母的数量?

names = {}

nametest = True
while nametest:
  
  name = input("Enter a name. Enter E to end. ")
  count = len(name)  # 获取字母的数量
  names[name] = count
  
  
  if name == 'E':
    print("You have entered", names.values())
 
    highest = max(zip(names.keys(), names.values()), key=lambda t : t[1])[0]
    
    print("Longest name was:", highest.upper(), "with", max(names.values()), "letters")

    break

希望这可以帮助你获取每个单词中字母的数量。

英文:

im trying to create a program that counts the number of letters in each word that is entered. I also want the program to print the one with the highest amount of letters and the word it corresponds with. I dont know how to count the number of letters in each word

this is my code so far. how do i get the number of letters?

names = {}

nametest = True
while nametest:
  
  name = input("Enter a name. Enter E to end. ")
  count = name.index(name)
  names[name] =count
  
  
  if name == 'E':
    print("You have entered", names.values())
 
    highest = max(zip(names.keys(), names.values()), key=lambda t : t[1])[0]
    
    print("Longest name was:",highest.upper(), "with", max(names.values()), "letters")

    break
    

答案1

得分: 2

你可以稍微编辑你的代码以使其正常工作

names = {}

while True:
name = input("输入一个名字。输入E结束:")
if name == 'E':
break
count = len(name)
names[name] = count

print("你已经输入了这些名字,这是它们的字母数:")
for name, count in names.items():
print(f"{name}: {count} 个字母")

longest_name = max(names, key=lambda x: names[x])
longest_count = names[longest_name]

print("最长的名字是:", longest_name.upper(), "有", longest_count, "个字母")


<details>
<summary>英文:</summary>

You can edit your code just a little bit to make it work

names = {}

while True:
name = input("Enter a name. Enter E to end: ")
if name == 'E':
break
count = len(name)
names[name] = count

print("You have entered these names and this is their letter count:")
for name, count in names.items():
print(f"{name}: {count} letters")

longest_name = max(names, key=lambda x: names[x])
longest_count = names[longest_name]

print("Longest name was:", longest_name.upper(), "with", longest_count, "letters")


</details>



huangapple
  • 本文由 发表于 2023年6月21日 23:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定