所以我試圖輸入一個輸入,將其轉換為美元,然后讓它告訴我它是多少張單獨的賬單。我有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
提交
取消