-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers_to_words.py
60 lines (42 loc) · 1.77 KB
/
numbers_to_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
zero=['zero','hundred']
one_digit=['one','two','three','four','five','six','seven','eight','nine']
ten_digits=['eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
last_digit=['ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
list1=[]
def num_length(x):
num_count=0
temp=x
while temp != 0:
temp1=temp%10
temp=int(temp/10)
list1.append(temp1)
num_count+=1
list1.reverse()
return num_count
def num_to_word(user_input):
try:
number_length=num_length(user_input)
if number_length>0 and number_length<=1:
print(one_digit[list1[0]-1])
elif number_length>0 and number_length<=2:
if user_input>10 and user_input<20:
print(ten_digits[list1[1]-1])
elif list1[1]==0:
print(last_digit[list1[0]-1])
else:
print(last_digit[list1[0]-1],one_digit[list1[1]-1])
elif number_length>0 and number_length<=3:
if list1[1]==0 and list1[2]==0 and list1[0]!=0:
print(one_digit[list1[0]-1], zero[list1[1]-1])
elif list1[2]==0:
print(one_digit[list1[0]-1], zero[1], last_digit[list1[1]-1])
elif list1[1]==1 and list1[2]==0 and list1[0]!=0:
print(one_digit[list1[0]-1],zero[list1[1]], ten_digits[list1[1]-1])
elif list1[1]==1 and list1[2]>0 and list1[2]<10:
print(one_digit[list1[0]-1], zero[1], ten_digits[list1[2]-1])
else:
print(one_digit[list1[0]-1], zero[1], last_digit[list1[1]-1], one_digit[list1[2]-1])
except:
print("its not an integer please enter a integer")
user_input=int(input("enter a three digit number"))
num_to_word(user_input)