snuffles101 - 4 months ago 12

Python Question

`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']

for i in List_of_numbers1to19:

print(i)

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 im trying to print a 3 different for loops at a different index, i have tried slicing the list, indexing the list, but i keep getting output for the numbers multipliable by 10 as the full list of List_of_numberstens. I think its clear what i am trying to do here.

Thanks in advanced for your help

Answer

I think you're overcomplicating the 20-100 case. From 20-100, numbers are very regular. (i.e. they come in the form `<tens_place> <ones_place>`

).

By using just one loop instead of nested loops makes the code simpler to follow. Now we just need to figure out what the tens place is, and what the ones place is.

The tens place can be easily found by using integer division by 10. (we subtract 2 since the list starts with twenty).

The ones place can similarly be found by using the modulo operator by 10. (we subtract 1 since the list starts with 1 and not 0).

Finally we just take care of the case of the ones place being 0 separately by using an if statement (and just not print any ones place value).

```
List_of_numbers1to19 = ['one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen',
'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen',
'nineteen']
List_of_numberstens = ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy',
'eighty', 'ninety']
for i in range(19):
print(List_of_numbers1to19[i])
for i in range(20, 100):
if i%10 == 0: #if multiple of ten only print tens place
print(List_of_numberstens[i//10-2]) #20/10-2 = 0, 30/10-2 = 1, ...
else: #if not, print tens and ones place
print(List_of_numberstens[i//10-2] + ' ' + List_of_numbers1to19[i%10-1])
```