浮云间
while由于浮点错误,您的程序卡在循环中。尝试在while循环中添加以下代码,您会看到 whileowed确实变得无限小,它永远不会变为零:...while owed > 0: print(owed) ...输出:...8.326672684688674e-178.326672684688674e-178.326672684688674e-178.326672684688674e-17...考虑将输入乘以100然后将其作为整数处理:owed = int(float(input("How much change is owed? $")) * 100)quarters = int(owed / 25)dimes = int((owed - quarters * 25) / 10)nickels = int((owed - quarters * 25 - dimes * 10) / 5)cents = int((owed - quarters * 25 - dimes * 10 - nickels * 5))coins = (quarters + dimes + nickels + cents)print('Quarters (${}): {}'.format(quarters*0.25, quarters))print('Dimes (${}): {}'.format(dimes*0.1, dimes))print('Nickels (${}): {}'.format(nickels*0.05, nickels))print('Cents (${}): {}'.format(cents, cents))print('Coins:', coins)或者,如果您想坚持使用贪心算法:owed = int(float(input("How much change is owed? $")) * 100)while owed > 0: if (owed - 25) >= 0: coins += 1 owed -= 25 elif (owed - 10) >= 0: coins += 1 owed -= 10 elif (owed - 5) >= 0: coins += 1 owed -= 5 elif (owed - 1) >= 0: coins += 1 owed -= 1coins = (quarters + dimes + nickels + cents)print('Quarters (${}): {}'.format(quarters*0.25, quarters))print('Dimes (${}): {}'.format(dimes*0.1, dimes))print('Nickels (${}): {}'.format(nickels*0.05, nickels))print('Cents (${}): {}'.format(cents, cents))print('Coins:', coins)输出>>> How much change is owed? $1.42Quarters ($1.25): 5Dimes ($0.1): 1Nickels ($0.05): 1Cents ($2): 2Coins: 9有关浮点限制的更多信息,请查看以下内容:https ://docs.python.org/3.8/tutorial/floatingpoint.html