Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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
    

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading