我正在考虑将货币值转换为字符串值。 示例:10675.78将转为“一万六千七百五十七和七十八美元
”我认为我做得比实际要复杂得多。有什么建议吗?
def money(value):
x = str(value)
if len(x)>8:
return ("Error. Value exceeds limit.")
elif len(x)==0:
return ("Error. Must enter a value.")
for len(x) in range(0,7):
for x[6:8]:
a = x[6:8]
print(a,'/100 dollars')
for x[3:5]:
b = x[4:6]
if b = '10'
tens = 'ten'
elif b = '11'
tens = 'eleven'
elif b = '12'
tens = 'twelve'
elif b = '13'
tens = 'thirteen'
elif b = '14'
tens = 'fourteen'
elif b = '15'
tens = 'fifteen'
elif b = '16'
tens = 'sixteen'
elif b = '17'
tens = 'seventeen'
elif b = '18'
tens = 'eighteen'
elif b = '19'
tens = 'nineteen'
else:
return false
for x[4]:
c = x[4]
if b = true:
fifth = ''
elif c = '9'
fifth = 'nine'
elif c = '8'
fifth = 'eight'
elif c = '7'
fifth = 'seven'
elif c = '6'
fifth = 'six'
elif c = '5'
fifth = 'five'
elif c = '4'
fifth = 'four'
elif c = '3'
fifth = 'three'
elif c = '2'
fifth = 'two'
elif c = '1'
fifth = 'one'
elif c = '0'
fifth = ''
for x[3]:
d = x[3]
if b = true:
fourth = ''
elif d = '9'
fourth = "ninety "
elif d = '8'
fourth = "eighty "
elif d = '7'
fourth = "seventy "
elif d = '6'
fourth = "sixty "
elif d = '5'
fourth = "fifty "
elif d = '4'
fourth = "forty "
elif d = '3'
fourth = "thirty "
elif d = '2'
fourth = "twenty "
elif d = '0'
fourth = ''
for x[2]:
e = x[2]
if e = '9'
third = 'nine hundred '
elif e = '8'
third = 'eight hundred '
elif e = '7'
third = 'seven hundred '
elif e = '6'
third = 'six hundred '
elif e = '5'
third = 'five hundred '
elif e = '4'
third = 'four hundred '
elif e = '3'
third = 'three hundred '
elif e = '2'
third = 'two hundred '
elif e = '1'
third = 'one hundred '
for x[0:2]:
j = [0:2]
if j = '10'
second = 'ten thousand '
elif j = '11'
second = 'eleven thousand '
elif j = '12'
second = 'twelve thousand '
elif j = '13'
second = 'thirteen thousand '
elif j = '14'
second = 'fourteen thousand '
elif j = '15'
second = 'fifteen thousand '
elif j = '16'
second = 'sixteen thousand '
elif j = '17'
second = 'seventeen thousand '
elif j = '18'
second = 'eighteen thousand '
elif j = '19'
second = 'nineteen thousand'
else:
return false
for x[1]:
g = x[1]
if j = true:
second = ''
elif g = '9'
second = 'nine thousand '
elif g = '8'
second = 'eight thousand '
elif g = '7'
second = 'seven thousand '
elif g = '6'
second = 'six thousand '
elif g = '5'
second = 'five thousand '
elif g = '4'
second = 'four thousand '
elif g = '3'
second = 'three thousand '
elif g = '2'
second = 'two thousand '
elif g = '1'
second = 'one thousand'
elif g = '0'
second = ''
for x[0]:
h = x[0]
if j = true:
first = ''
elif h = '9'
first = 'ninety '
elif h = '8'
first = 'eighty '
elif h = '7'
first = 'seventy '
elif h = '6'
first = 'sixty '
elif h = '5'
first = 'fifty '
elif h = '4'
first = 'fourty '
elif h = '3'
first = 'thirty '
elif h = '2'
first = 'twenty '
答案 0 :(得分:1)
我没有看到for循环的任何用法,对于数字的每个部分,你只重复一次动作。
此外,对于更干净的代码,我会使用字典来保存复杂的结构,例如:
hDict = {'2':'twenty', '3':'thirty',...}
first=hDict[x[0]]
等等。