楼主: zrz_108
4418 9

[问答] 【新手】请教有关“找零钱”的习题。 [推广有奖]

已卖:37份资源

副教授

45%

还不是VIP/贵宾

-

威望
0
论坛币
5653 个
通用积分
568.4491
学术水平
35 点
热心指数
51 点
信用等级
34 点
经验
173690 点
帖子
393
精华
0
在线时间
1318 小时
注册时间
2008-3-19
最后登录
2024-3-22
毕业学校
吉林大学

楼主
zrz_108 发表于 2014-11-22 16:00:05 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
几天前,开始学习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
二维码

扫码加我 拉你入群

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

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

关键词:Quarters Quarter change Twenty Nickel 编写程序 person change

本帖被以下文库推荐

那没有看见就相信的有福了。

沙发
bndnsuy 发表于 2014-11-23 11:03:06
“习题”长的怎么样?
先把你的原始问题描述给人看一下,方便别人了解问题始末。

藤椅
zrz_108 发表于 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

板凳
zrz_108 发表于 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

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

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


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

虽然它正确的值其实应该是1,但因之前经过这行的运算
  1. penny = (change - twenty * 20 - ten * 10 -five * 5 - one * 1 - quarter * 0.25 - dime * 0.10 - nickel * 0.05) / 0.01
复制代码

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


以下的代码,并不是针对该习题的直接解答。我加了round()(不知书中是否有提到?),并在每找一种零钱,就显示一次还有多少余额,这是为了方便观察代码逐次执行过后的结果。供你参考
你也可试试把round()拿掉,观察一下结果如何
  1. cost = 65.64
  2. given = 100
  3. change = given - cost
  4. twenty = change // 20
  5. remain = round(change - 20 * twenty, 2)
  6. print('找20元纸钞:{:.0f}张,余额:{}'.format(twenty, remain))
  7. ten = remain // 10
  8. remain = round(remain - 10 * ten, 2)
  9. print('找10元纸钞:{:.0f}张,余额:{}'.format(ten, remain))
  10. five = remain // 5
  11. remain = round(remain - 5 * five, 2)
  12. print('找5元铜板:{:.0f}个,余额:{}'.format(five, remain))
  13. one = remain // 1
  14. remain = round(remain - 1 * one, 2)
  15. print('找1元铜板:{:.0f}个,余额:{}'.format(one, remain))
  16. quarter = remain // 0.25
  17. remain = round(remain - 0.25 * quarter, 2)
  18. print('找25分铜板:{:.0f}个,余额:{}'.format(quarter, remain))
  19. dime = remain // 0.10
  20. remain = round(remain - 0.1 * dime, 2)
  21. print('找10分铜板:{:.0f}个,余额:{}'.format(dime, remain))
  22. nickel = remain // 0.05
  23. remain = round(remain - 0.05 * nickel, 2)
  24. print('找5分铜板:{:.0f}个,余额:{}'.format(nickel, remain))
  25. penny = remain // 0.01
  26. remain = round(remain - 0.01 * penny, 2)
  27. print('找1分铜板:{:.0f}个,余额:{}'.format(penny, remain))
复制代码


已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
zrz_108 + 5 + 5 + 5 好的意见建议
狂热的爱好者 + 100 + 100 + 5 + 5 + 5 不能更赞~~~

总评分: 经验 + 100  论坛币 + 100  学术水平 + 10  热心指数 + 10  信用等级 + 10   查看全部评分

地板
bndnsuy 发表于 2014-11-24 21:39:09
老天!回覆这问题,竟花了我一个多小时的时间

7
狂热的爱好者 学生认证  发表于 2014-11-24 22:35:24
bndnsuy 发表于 2014-11-24 21:39
老天!回覆这问题,竟花了我一个多小时的时间
辛苦辛苦~~~
[em17][em17]

8
zrz_108 发表于 2014-11-25 01:27:21
bndnsuy 发表于 2014-11-24 21:29
不知这是哪本书第几章节的习题?可以告知一下吗?
会问这个问题,是因为书中的习题应该是用来测试读者对书 ...
谢谢你的建议!

另外,我在另一个网站也得到一些启示,就是有关“钱”的问题的时候,最好把小数点变成整数,计算各零钱的数量。以下是按照这个思路改写的代码,供参考!
  1. cost = float(input("How much did the item cost: "))
  2. given = float(input("How much did the person give you: "))
  3. change = (given - cost) * 100
  4. twenty = change // 2000
  5. ten = change % 2000 // 1000
  6. five = change % 2000 % 1000 // 500
  7. one = change % 2000 % 1000 % 500 // 100
  8. quarter = change % 2000 % 1000 % 500 % 100 // 25
  9. dime = change % 2000 % 1000 % 500 % 100 % 25 // 10
  10. nickel = change % 2000 % 1000 % 500 % 100 % 25 % 10 // 5
  11. penny = change % 2000 % 1000 % 500 % 100 % 25 % 10 % 5 // 1
  12. print("The person's change is %1.2f"%(change))
  13. print("The bills or the change should be:")
  14. print(int(twenty), "twenties")
  15. print(int(ten), "tens")
  16. print(int(five), "fives")
  17. print(int(one), "ones")
  18. print(int(quarter), "quarters")
  19. print(int(dime), "dimes")
  20. print(int(nickel), "nickels")
  21. print(int(penny), "pennies")
复制代码


这本书在论坛里面有,现把其中的一个链接地址粘贴下,需要的,可以再搜索找个更便宜的!https://bbs.pinggu.org/forum.php?mod=viewthread&tid=2305806
那没有看见就相信的有福了。

9
bndnsuy 发表于 2014-11-25 22:25:04
这是Python Programming Fundamentals第一章的习题
稍翻了一下第一章内容
我在前面提出的第一点,在第22页1.18图表中
第二点应该在那一小节也解释了

这问题其实只用第一章教的东西就可解决了
重点是把这章内容消化了,依作者引导的思路阅读本书,如果你相信作者写的书不错话,建议这么做


至于你提到把小数变成整数的做法
也许是源自于别网站使用其它编程语言的情形
个人并不太建议
一个简单的理由是,这样做有点像绕远路的做法,并不符合Python的理念。(也许书中也会提到Python Philosophy,或The Zen of Python)

还有其它的理由,只稍谈几个:
这会让我觉得这种语言对于浮数的处理并不周全,所以要采用这种迂回的方式;或是编程者对语言不够熟悉
如果是前者,语言本身应该要提供相关工具,以方便处理这类工具
至于后者,就是再深入了解一下这个编程语言就得了


我用过某种特殊用途的语言,并没有整数型态,数值一律是以浮点数处理
在数值比较大小时,就发现问题,比如两个变量,存放的应该都是3.0的值,但比较起来却不相等
原因在于经过一些运算后,一个是2.999999999,另一个是3.00000001
所以我也做了一些「特殊程序」来处理这类数值比较的工作
但是一阵子过后,我逐渐体会到这语言为何会有这样的设计?为何没有整数型态?
因为它用于「特殊用途」,的确是用不着整数的
而且我也开始了解到我之前用错误的角度来使用这语言
而是应该以这语言的设计角度来看问题
换个角度后,以前的不解,就有了新的看法,也就不再执着于那些「特殊程序」了
举这个例子,就是前述后者的一例


如果你之前学过其它编程语言,也建议先别把其它编程言的观念带进来
先放空自己,如果你相信这本书是个好教材的话(我并不清楚本书写的如何),先照它的引导逐步学习,试着只用它教的东西来解算习题,这样才会了解自己从中了解多少

如果书的内容编写的很好,若习题不会作,就表示可能还没读透;否则就有可能是书编写的不好

10
zrz_108 发表于 2014-11-26 06:14:35
bndnsuy 发表于 2014-11-25 22:25
这是Python Programming Fundamentals第一章的习题
稍翻了一下第一章内容
我在前面提出的第一点,在第22页 ...
原来如此!
谢谢你的建议,受教了!

我是第一次学习编程语言,以前没学过别的。既然这样,我得完全按照python思路来解决问题了。
以后如有难题,我再向你请教吧!

另外,这本书感觉在所有的教材中讲得最简单,而且还有例题和习题也讲得比较深入浅出,所以才选择这个来自学。也不知道合不合适。
还有一个重要原因是,这本书是用python3.x讲解的书中最简单的。

以后还望多多指教啊!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 13:03