Write a python program that can calculate the total number of letters entered in a collection of words. The program must ask the user for input until the user either types “quit” or “X” in upper or lower case. The number of letters in each word is displayed and the total of all letters in all words (excluding quit or X) is displayed at the end.
Answer
here we ask input from user, user can input more as they wish. when user input x or X or quit it wil break the loop. So here each input is collected to an list. So we can easly find the length of each word in a list. and also can find the length of entire length of word.
So here is the code,
words=[] #an empty list, value will come during execution.
total=0
while 1:
w=input() #user can input the value as they wish
if w=="quit" or w=='X' or w=='x': #when user enter x or quit loop will break
break
else:
words.append(w)
#using for loop we can access each word in a list. then can find the length.
for word in words:
total=total+len(word)
print(word+"(length)="+str(len(word)))
print("total words="+str(total))
output
Any doubt please comment
Thanks in advance
Get Answers For Free
Most questions answered within 1 hours.