Question

In Python, write a function numLetters() that keeps prompting the user for words until they hit...

In Python, write a function numLetters() that keeps prompting the user for words until they hit return (using while loops). The function should then return the percent of 3-letter words that were entered. So, if a user entered ten words and 4 of the words had 3 letters, then the percent of 3-letter words is 40

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def numLetters():
   wordsInput=[]   #list for storing words
   #reading words until enter return
   while(True):
       inpt=input("Enter word :")
       if(inpt!="return"):
           wordsInput.append(inpt)
       else:
           break
  
   wordsDigitCount=[] #for storing every word count
   #counting every word count
   for word in wordsInput:
       dcount=0
       for ch in word:
           if(ch.isspace()!= True):
               dcount=dcount+1
       wordsDigitCount.append(dcount)

   #calculating 3 letter word count and percentage
   threeLetter=0
   for i in range(len(wordsDigitCount)):
       if(wordsDigitCount[i]==3):
           threeLetter=threeLetter+1
   return len(wordsInput),threeLetter*10   #returning count of total words and percentage of 3 letter words
      
#main program
totalWords,percentage=numLetters() #storing function return values
print("out of {} words, 3-letter percentage : {}".format(totalWords,percentage))

  

Add a comment
Know the answer?
Add Answer to:
In Python, write a function numLetters() that keeps prompting the user for words until they hit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT