要求金额和显示金额的程序,以美元,季度,硬币等为单位

时间:2014-02-23 04:27:06

标签: python python-3.x

在Python中,这是我到目前为止所拥有的:

def enter():
    amount=float(input('Enter cost:'))
    remainder=amount*100
    dollars(remainder)
    quarters(remainder)
    dimes(remainder)
    nickels(remainder)
    cents(remainder)
def dollars(remainder):
    dollars=remainder//100
    print('Dollars:', dollars)
def quarters(remainder):
    quarters=remainder//25
    print('Quarters:',quarters)
def dimes(remainder):
    dimes=remainder//10
    print('Dimes:', dimes)
def nickels(remainder):
    nickels=remainder//5
    print('Nickels:', nickels)
def cents(remainder):
    cents=remainder//1
    print('Cents:', cents)
enter()

问题是当输入金额时,例如5.25,它会显示: 5.0美元, 21.0季度

我需要它来显示: 5.0美元, 1.0季度

1 个答案:

答案 0 :(得分:0)

你可以这样做:

def enter():
    amount=float(input('Enter cost:'))
    remainder=amount*100
    dollar=dollars(remainder)
    remainder-=dollar
    print('Dollars:', dollar)
    quarter=quarters(remainder)
    ......

def dollars(remainder):
    dollars=remainder//100
    return dollars
.....