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', 'nineteen'] list_of_numbers1to9 = list_of_numbers1to19[0:9] list_of_numberstens = ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] in list_of_numbers1to19: print(i) list_of_numbers21to99 = [] count = 19 tens_count = 0 j in list_of_numberstens: k in list_of_numbers1to9: if tens_count%10 == 0: #should print iteration of list_of_numberstens tens_count +=1 tens_count +=1 print(j, k)
as can see, getting messy :p sorry that. i'm trying print them using 3 different for-loops different index. have tried slicing list , indexing list, keep getting output numbers multipliable 10 full list of list_of_numberstens
.
i think it's clear i'm trying here.
thanks in advance help!
i know accepted answer, particularly mentioned nested loops - doesn't use - , you're missing what's great python's iteration , not needing kind of i//10-2
, print(j,k)
stuff work out indexes lists.
python's for
loop iteration runs on items in list directly , can print them, 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'] word in digits + teens: print(word) tens_word in tens: print(tens_word) # e.g. twenty digits_word in digits: print(tens_word, digits_word) # e.g. twenty 1 print("one hundred")
Comments
Post a Comment