为了账号安全,请及时绑定邮箱和手机立即绑定

将总金额转换为单独的票据 Python

将总金额转换为单独的票据 Python

皈依舞 2021-07-09 19:07:27
所以我试图输入一个输入,将其转换为美元,然后让它告诉我它是多少张单独的账单。我有def read_exchange_rates():    answer={}    answer['USD'] = 1    answer['GBP'] = 0.76    return answerclass Money:    exchange_rates = read_exchange_rates()    def __init__ (self, monamount, code):        self.monamount=monamount        self.code=code    def to(self, othercode):        i = self.monamount/self.exchange_rates[self.code]        j = i*self.exchange_rates[othercode]        return j    def __str__(self):        return str(self.code)+' '+str(self.monamount)    def bills(self):        j=self.to('USD')        hundred=j//100        return hundred        jwohundred=j-100*hundred        return jwohundred        fifty=jwohundred//50        return fifty        jwofifty=jwohundred-fifty*50        return jwofifty        twenty=jwofifty//20        return twenty        jwotwenty=jwofifty-twenty*20        ten=jwotwenty//10        return ten        jwoten=jwotwenty-ten*10        return jwoten        five=jwoten//5        return five        jwofive=jwoten-five*5        return jwofive        one=jwofive//1        return one        jwoone/jwofive-one*1        return jwooone        print('The bills/noted for USD'+' '+str(j)+' are:')        print('USD 100 = '+str(jwohundred))        print('USD 50 = '+str(jwofifty))        print('USD 20 = '+str(jwotwenty))        print('USD 10 = '+str(jwoten))        print('USD 5 = '+str(jwofive))        print('USD 1 = '+str(jwoone)) 如果我先做 a=Money(145.1,'GBP') 然后是 a.bills(),返回的就是 1.0。它应该返回The bills for USD 220.08 are:USD 100 = 2 USD 50 = 0USD 20 = 1USD 10 = 0USD 5 = 0USD 1 = 0我做错了什么?我知道有一种方法可以使用字典,但我不知道如何使用。谢谢你。
查看完整描述

2 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

当一个函数返回它时,它就完成了执行。这里只会执行第一条语句。

如果您唯一关心的是最后的print语句,只需去掉 return 语句。如果你需要返回东西,你可以在最后一个语句中将它们一起返回:

return jwohundred, jwofifty, jwotwenty, jwoten, jwofive, jwoone


查看完整回答
反对 回复 2021-07-13
  • 2 回答
  • 0 关注
  • 231 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号