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

所以我试图输入一个输入,将其转换为美元,然后让它告诉我它是多少张单独的账单。我有


def read_exchange_rates():

    answer={}

    answer['USD'] = 1

    answer['GBP'] = 0.76

    return answer

class 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 = 0

USD 20 = 1

USD 10 = 0

USD 5 = 0

USD 1 = 0

我做错了什么?我知道有一种方法可以使用字典,但我不知道如何使用。谢谢你。


皈依舞
浏览 232回答 2
2回答

慕哥9229398

当一个函数返回它时,它就完成了执行。这里只会执行第一条语句。如果您唯一关心的是最后的print语句,只需去掉 return 语句。如果你需要返回东西,你可以在最后一个语句中将它们一起返回:return jwohundred, jwofifty, jwotwenty, jwoten, jwofive, jwoone
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python