snuffles101 snuffles101 - 1 year ago 89
Python Question

Printing numbers from 1-100 as words in Python 3

List_of_numbers1to19 = ['one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen',
'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen',
List_of_numbers1to9 = List_of_numbers1to19[0:9]
List_of_numberstens = ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy',
'eighty', 'ninety']

for i in List_of_numbers1to19:
list_of_numbers21to99 = []
count = 19
tens_count = 0
for j in List_of_numberstens:
for k in List_of_numbers1to9:
if tens_count%10 == 0:
#should print an iteration of List_of_numberstens
tens_count +=1
tens_count +=1
print(j, k)

As you can see, this is getting messy :P So sorry for that.
Basically I'm trying to print them using three different for-loops with a different index. I have tried slicing the list and indexing the list, but I keep getting output for the numbers multipliable by 10 as the full list of

I think it's clear what I'm trying to do here.

Thanks in advance for your help!

Answer Source

I know you already accepted an answer, but you particularly mentioned nested loops - which it doesn't use - and you're missing what's great about Python's iteration and not needing to do that kind of i//10-2 and print(j,k) stuff to work out indexes into lists.

Python's for loop iteration runs over the items in the list directly and you can just print them, so I answer:

digits = ['one', 'two',   'three', 'four', 'five', 
          'six', 'seven', 'eight', 'nine']

teens  = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 
          'sixteen', 'seventeen', 'eighteen', 'nineteen']

tens   = ['twenty', 'thirty', 'fourty', 'fifty', 
          'sixty', 'seventy', 'eighty', 'ninety']

for word in digits + teens:

for tens_word in tens:
    print(tens_word)        # e.g. twenty

    for digits_word in digits:
        print(tens_word, digits_word)   # e.g. twenty one

print("one hundred")

Try it online at

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download