所以我试图输入一个输入,将其转换为美元,然后让它告诉我它是多少张单独的账单。我有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
添加回答
举报
0/150
提交
取消