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