全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
4347 9
2014-11-22
几天前,开始学习Python,有一道习题,我编写程序后总是得不到结果。(习题在3楼贴上了)
其他的都对,最后一美分的数量总是少一个。为什么呢?
我的代码如下:

cost = float(input("How much did the item cost: "))
given = float(input("How much did the person give you: "))
change = given - cost
twenty = change // 20
ten = (change - twenty * 20) // 10
five = (change - twenty * 20 - ten * 10) // 5
one = (change - twenty * 20 - ten * 10 -five * 5) // 1
quarter = (change - twenty * 20 - ten * 10 -five * 5 - one * 1) // 0.25
dime = (change - twenty * 20 - ten * 10 -five * 5 - one * 1 - quarter * 0.25) // 0.10
nickel = (change - twenty * 20 - ten * 10 -five * 5 - one * 1 - quarter * 0.25 - dime * 0.10) // 0.05
penny = (change - twenty * 20 - ten * 10 -five * 5 - one * 1 - quarter * 0.25 - dime * 0.10 - nickel * 0.05) / 0.01
print("The person's change is %1.2f"%(change))
print("The bills or the change should be:")
print(int(twenty), "twenties")
print(int(ten), "tens")
print(int(five), "fives")
print(int(one), "ones")
print(int(quarter), "quarters")
print(int(dime), "dimes")
print(int(nickel), "nickels")
print(int(penny), "pennies")


当然,中间定义各面值纸币、硬币的公式可以用别的,这是我几经修改尝试后的最后版本。结果都一样,就是一美分的少一个。好郁闷啊!

另外,请回答的朋友们,别用条件等高级代码。
就用最最基本的加减乘除等运算符号。
我用的版本是Python 3.4
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-11-23 11:03:06
“习题”长的怎么样?
先把你的原始问题描述给人看一下,方便别人了解问题始末。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-11-24 08:53:19
Write a program that computes the minimum number of bills and coins needed to make change for a person. For instance, if you need to give $34.36 in change you would need one twenty, one ten, four ones, a quarter, a dime, and a penny. You don’t have to
compute change for bills greater than $20 dollar bills or for fifty cent pieces. You can solve this problem by doing division, multiplication, subtraction, and converting floats to ints when appropriate. So, when you run the program it should look exactly like this:
How much did the item cost: 65.64
How much did the person give you: 100.00
The person's change is $34.36
The bills or the change should be:
1 twenties
1 tens
0 fives
4 ones
1 quarters
1 dimes
0 nickels
1 pennies
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-11-24 09:00:28
bndnsuy 发表于 2014-11-23 11:03
“习题”长的怎么样?
先把你的原始问题描述给人看一下,方便别人了解问题始末。
Write a program that computes the minimum number of bills and coins needed to make change for a person. For instance, if you need to give 34.36 in change you would need one twenty, one ten, four ones, a quarter, a dime, and a penny. You don′t have to compute change for bills greater than 20 dollar bills or for fifty cent pieces. You can solve this problem by doing division, multiplication, subtraction, and converting floats to ints when appropriate. So, when you run the program it should look exactly like this:
How much did the item cost: 65.64
How much did the person give you: 100.00
The person's change is $34.36
The bills or the change should be:
1 twenties
1 tens
0 fives
4 ones
1 quarters
1 dimes
0 nickels
1 pennies
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-11-24 21:29:54
不知这是哪本书第几章节的习题?可以告知一下吗?
会问这个问题,是因为书中的习题应该是用来测试读者对书中该章节的了解程度
也就是说,习题会与该章节内容,甚至更早的内容有关

题目中只提示使用除、乘、除,将浮点数转成整数
但我不知书中是否有提到:
一、使用int()把浮点数转成整数时,其作用有何影响?
二、Python处理浮点数的方式?


你的脚本中原本输入2个整数,经过几次运算后,后来的变量中装的都是浮点数
把最后一行的print()中的int()拿掉,看看输出可能会像这样:
复制代码

虽然它正确的值其实应该是1,但因之前经过这行的运算
复制代码

会成为上述接近于1,但不足1的浮点数
这个不足1的浮点数,再用int()一转换,就成0了。你可以试试int(3.9)的结果是多少?
谈的这些,就是我上述所提的二点问题


以下的代码,并不是针对该习题的直接解答。我加了round()(不知书中是否有提到?),并在每找一种零钱,就显示一次还有多少余额,这是为了方便观察代码逐次执行过后的结果。供你参考
你也可试试把round()拿掉,观察一下结果如何
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-11-24 21:39:09
老天!回覆这问题,竟花了我一个多小时的时间
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群