1

I have the following question for one of my classes but I am a bit stuck. I have created the below code but when I print it only prints the count of the last word only not for each word.

"Use a nested loop to count the letters of each element in the nmrls list, then print the element along with its number of letters."

!pip install inflect
import inflect


nmrls = inflect.engine()
x=[]
for i in range (0,22):
    x.append(nmrls.number_to_words(i))   
for nmrls in x:
    count=0
    for letter in x:
        count+=1
    print(nmrls, count)
    
3
  • what is the content of x after you add the words ? Commented Oct 12, 2020 at 17:51
  • 2
    You're looping over x twice, so the count you're getting is actually the number of items inside of x, not the number of letters in nmrls. Commented Oct 12, 2020 at 17:52
  • for letter in x: => for letter in nmrls: Commented Oct 12, 2020 at 17:53

1 Answer 1

1

Found the issue :) Looping over x twice was overriding the value.

!pip install inflect
import inflect


nmrls = inflect.engine()
x=[]
for i in range (0,22):
    x.append(nmrls.number_to_words(i))   
for nmrls in x:
    count=0
    for letter in nmrls:
        count+=1
    print(nmrls, count)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.