打印输出时放负号

时间:2019-02-05 05:28:37

标签: python output python-3.7 negative-number

我想要一个小建议。我试图在程序输出的前面写“负”,如果输入中有任何负数,但不能这样做。我的代码如下:

def word(num):
   d1= {0:'Zero',1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen',14:'Fourteen',15:'Fifteen',16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Ninteen',20:'Twenty',30:'Thirty',40:'Fourty',50:'Fifty',60:'Sixty',70:'Seventy',80:'Eighty',90:'Ninty'}

   if (num<20):
      return d1[num]
   if (num<100):
      if num % 10 == 0:
         return d1[num]
      else:
         return d1[num // 10 * 10] + ' ' + d1[num % 10]
   if (num < 0):
      return "negative" + word(num)

print (word(-123))

当我运行此程序时,它显示回溯错误(关键错误:-123)。 我该怎么做打印:

负一百二十三? 谢谢。

0 个答案:

没有答案