我正在尝试编写一个将数字转换为单词的代码,最多可达999兆。这是我的代码到目前为止。它工作到119,但之后事情变得混乱。我不能使用append或枚举。我被困在如何打印更大的数字;我将如何格式化一个数字,如978,674,237,105?python:将数字转换为单词
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