我正在尝试编写一个代码,可以将数字转换为单词,达到999万亿的级别。 这是我目前的代码。它可以工作到119,但之后就会变得混乱。 我不能使用append或enumerate。 我被卡住了,不知如何打印更大的数字; 我该如何格式化像978,674,237,105这样的数字?
NUMBERS = ["zero", "one", "two","three","four","five","six","seven","eight","nine",
"ten","eleven","twelve","thirteen","fourteen","fiveteen","sixteen",
"seventeen","eightteen","nineteen"]
TENS = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
"ninety"]
HUNNITS = ["","hundred","thousand","million","billion","trillion"]
n = eval(input("What is the number the you want to convert? "))
def convert():
if n >= 20:
tens = n // 10
units = n % 10
if units != 0:
result = TENS[tens] + "-" + NUMBERS[units]
else:
result = TENS[tens]
else:
result = NUMBERS[n]
print (result)
def convert2():
if n >=100:
tens2 = n//100
units2 = n%100
if units2 != 0:
result2 = HUNNITS[tens2] + "-" + TENS[tens2] + "and" + NUMBERS[units2]
else:
result2 = HUNNITS[tens2]
else:
result2 = HUNNITS[n]
print(result2)
def main():
if n >=20 and n< 100:
x = convert()
if n >=100:
y = convert2()
main()
eval(input())
,将输入转换为整数(或浮点数)。 - timgeb