Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python十进制数_Python_Python 3.x_Decimal - Fatal编程技术网

Python十进制数

Python十进制数,python,python-3.x,decimal,Python,Python 3.x,Decimal,我刚刚开始学习编程,我正在尝试用Python编写一个程序,它计算应该支付的金额和实际支付的金额,并计算出纳应该返还哪些纸币和硬币 除了低于一美元的金额外,它似乎对所有东西都有效。我怀疑这与十进制数有关,但我找不到错误。另外,对不起我的代码,它真的很混乱,可能只需要几行就可以完成,但我完全是初学者 amount_due = float(input('What is the amount you are supposed to pay? ')) amount_paid = float(input('

我刚刚开始学习编程,我正在尝试用Python编写一个程序,它计算应该支付的金额和实际支付的金额,并计算出纳应该返还哪些纸币和硬币

除了低于一美元的金额外,它似乎对所有东西都有效。我怀疑这与十进制数有关,但我找不到错误。另外,对不起我的代码,它真的很混乱,可能只需要几行就可以完成,但我完全是初学者

amount_due = float(input('What is the amount you are supposed to pay? '))
amount_paid = float(input('What is the amount you paid? '))

one_hundreds = 0
twenties = 0
tens = 0
fives = 0
ones = 0
quarter = 0
dime = 0
nickel = 0
penny = 0

def calculate_difference():
    global difference
    difference = amount_paid - amount_due
    difference = float(difference)

def evaluate_difference():
    if difference < 0:
        print("Sorry, you haven't paid enough money.")
        quit()
    elif difference == 0:
        print('Awesome! You paid exactly how much you were supposed to pay.')
    else: 
        check_hundreds()

def check_hundreds():
    global one_hundreds
    global difference
    while difference >= 100:
        one_hundreds += 1
        difference -= 100
    else:
        check_twenties()

def check_twenties():
    global twenties
    global difference
    while difference >= 20:
        twenties += 1
        difference -= 20
    else:
        check_tens()

def check_tens():
    global tens
    global difference
    while difference >= 10:
        tens += 1
        difference -= 10
    else:
        check_fives()

def check_fives():
    global fives
    global difference
    while difference >= 5:
        fives += 1
        difference -= 5
    else:
        check_ones()

def check_ones():
    global ones
    global difference
    while difference >= 1:
        ones += 1
        difference -= 1
    else:
        check_quarters

def check_quarters():
    global quarter
    global difference
    while difference >= 0.25:
        quarter += 1
        difference -= 0.25
    else:
        check_dimes()

def check_dimes():
    global dime
    global difference
    while difference >= 0.1:
        dime += 1
        difference -= 0.1
    else:
        check_nickels()

def check_nickels():
    global nickel
    global difference
    while difference >= 0.05:
        nickel += 1
        difference -= 0.05
    else:
        check_pennies()

def check_pennies():
    global penny
    global difference
    while difference >= 0.01:
        penny += 1
        difference -= 0.01

def write_results():
    global one_hundreds
    global twenties
    global tens
    global fives
    global ones
    global quarter
    global dime
    global nickel
    global penny

    print('The cashier should return you ' + str(one_hundreds) + " one hundred dollar bills, " + str(twenties) + ' twenty dollar bills, ' + str(tens) + ' ten dollar bills, ' + str(fives) + ' five dollar bills, ' + str(ones) + ' one dollar bills, ' + str(quarter) + ' quarters, ' + str(dime) + ' dimes, ' + str(nickel) + ' nickels and ' + str(penny) + ' pennies.')

        
calculate_difference()
evaluate_difference()
write_results()
amount\u due=float(输入('您应该支付的金额是多少?'))
支付金额=浮动(输入('您支付的金额是多少?'))
百分之一=0
二十岁=0
十=0
五=0
一=0
四分之一=0
一角=0
镍=0
便士=0
def计算_差值():
全球差异
差额=已付金额-到期金额
差异=浮动(差异)
def评估_差异():
如果差值<0:
打印(“对不起,你付的钱不够。”)
退出
elif差==0:
打印(“太棒了!你付了你应该付的钱。”)
其他:
支票
def check_QUOTES():
全球一百人
全球差异
当差值>=100时:
百分之一+=1
差额-=100
其他:
支票(二十)
def check_二十岁():
全球二十年代
全球差异
当差值>=20时:
二十岁+=1
差额-=20
其他:
检查
def check_tens():
全球十位数
全球差异
当差值>=10时:
十位数+=1
差值-=10
其他:
支票(五)
def check_fives():
全球五人制
全球差异
当差值>=5时:
五+=1
差值-=5
其他:
勾选
def check_ones():
全球的
全球差异
差异>=1时:
一+=1
差值-=1
其他:
检查宿舍
def check_QUOTES():
全球季度
全球差异
当差值>=0.25时:
四分之一+=1
差值-=0.25
其他:
支票
def check_dimes():
全球一角硬币
全球差异
差异>=0.1时:
一角硬币+=1
差值-=0.1
其他:
支票(五分硬币)
def check_nickels():
全球镍
全球差异
差异>=0.05时:
镍+=1
差值-=0.05
其他:
支票
def check_pennies():
全球便士
全球差异
当差值>=0.01时:
便士+=1
差值-=0.01
def write_results():
全球一百人
全球二十年代
全球十位数
全球五人制
全球的
全球季度
全球一角硬币
全球镍
全球便士
打印('出纳应退还您'+str(一百元)+“一百元钞票”、+str(二十元)+“二十元钞票”、+str(十元)+“十元钞票”、+str(五元)+“五元钞票”、+str(一元)+“一元钞票”、+str(四分之一)+“四分之一”、+str(十分之一)+“十分之一”、+str(五分之一)和'+str(便士)+“便士”。)
计算_差()
评估_差异()
写入结果()
检查完后,您忘了放()

附言。
使用globals是一种非常糟糕的编程风格

问题在于,对于小数点,浮点指针数字并不总是准确的。更多关于它的信息。这就是为什么Python中存在
Decimal
check\u quarters
内的
check\u ones
行应该是
check\u quarters()
。非常感谢。为什么是globals?当你的程序有bug时,通常意味着某个变量的值是错误的。如果您使用globals,很难判断是什么使这个变量持有错误的值,因为代码中的任何地方都可以使用“global”来更改这个变量