2013-10-16 114 views
-1

我在做假设将货币值转换为字符串值。 例如:10675.78将转向“10675和78/100美元python简单的钱代码初学者

我觉得我在做这个比它需要更复杂的是任何建议

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

Python中的所有for循环都需要与它们一起“in”。 'for x [3:5]:'会爆炸。它应该是:'var for x [3:5]:'(我只是选了'var')。此外,你使用'=='进行比较测试,而不是'='(这是用于变量赋值)。 – iCodez

+0

注意到这个 - http://stackoverflow.com/questions/8982163/how-do-i-tell-python-to-convert-integers-into-words - 似乎有一个pynum2word模块在那里。 '导入num2word',然后'打印num2word.to_card(10675.78)'。否则,我会避免这些if-elif集群并去查找表。 IE,几十= {10:'十',11:'十一'...}并且以这种方式匹配。 – pp19dd

回答

1

我?没有看到任何使用的for循环,人数的每一部分你重复动作一次

此外,清晰的代码,我会用字典,将保存复杂,如果结构,如:

hDict = {'2':'twenty', '3':'thirty',...} 

first=hDict[x[0]] 

等。