简体   繁体   中英

Python Currency Converter

The following code is a simple currency converter. Everything works but the last if statement, which causes the code to break. Whenever I put a number less than .25 in an if statement, it breaks the code. I would greatly appreciate any input to why this is. Thank you very much.

def currency_converter(amount):
        hundred = 0
        fifty = 0
        ten = 0
        five = 0
        one = 0
        quarter = 0
        dime = 0
        while amount > 0:
                if amount >= 100:
                        hundred+=1
                        amount-=100
                if 100 > amount >= 50:
                        fifty+=1
                        amount-=50
                if 50 > amount >= 10:
                        ten+=1
                        amount-=10
                if 10 > amount >= 5:
                        five+=1
                        amount-=5
                if 5 > amount >= 1:
                        one+=1
                        amount-=1
                if 1 > amount >= float(.25):
                        quarter+=1
                        amount-=float(.25)
                if float(.25) > amount >= float(.1):
                        dime+=1
                        amount-=float(.1)
        print(hundred, "Hundred-dollar bill")
        print(fifty, "Fifty-dollar bill")
        print(ten, "Ten-dollar bill")
        print(five, "Five-dollar bill")
        print(one, "One-dollar bill")
        print(quarter, "Quarter")      
        print(dime, "Dime")
currency_converter(5.6)
def currency_converter(amount):
  amount = int(100*amount)
  hundred = (amount//10000)
  amount -= hundred*10000
  fifty = (amount//5000)
  amount -= fifty*5000
  ten = (amount//1000)
  amount -= ten*1000
  five = (amount//500)
  amount -= five*500
  one = (amount//100)
  amount -= one*100
  quarter = (amount//25)
  amount -= quarter*25
  dime = (amount//5)
  amount -= dime*5
  penny = amount
  return (hundred, fifty, ten, five, one, quarter, dime,penny)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM