我的任务是

"编写一个功能selectCoins,要求用户输入金额

(以便士为单位),然后输出每种面额的硬币数量(从£ 2向下

到1p)应该用来精确地补足该金额(使用尽可能少的

硬币数量)。 例如,如果输入为292,则该函数应报告:

1×£ 2、0×£ 1、1×50p,2×20p,0×10p,0×5p,1×2p,0×1p。 (提示:使用整数

除法和余数)。"

def selectCoins():

twopound = 200

onepound = 100

fiftyp = 50

twentyp = 20

tenp = 10

fivep = 5

twop = 2

onep = 1

a = 0

b = 0

c = 0

d = 0

e = 0

f = 0

g = 0

h = 0

money = int(input('Enter how much money you have in pence'))

while True:

if money >= twopound:

money = money - twopound

a = a + 1

elif money >= onepound:

money = money - onepound

b = b + 1

elif money >= fiftyp:

money = money - fiftyp

c = c + 1

elif money >= twentyp:

money = money - twentyp

d = d + 1

elif money >= tenp:

money = money - tenp

e = e + 1

elif money >= fivep:

money = money - fivep

f = f + 1

elif money >= twop:

money = money - twop

g = g + 1

elif money >= onep:

money = money - onep

h = h + 1

else:

money = 0

break

print(a,b,c,d,e,f,g,h)

我是编程新手,因此在运行此代码时,它只是输入

当我键入292而不是它应输出的内容时,为" 1 0 0 0 0 0 0 0 0"。

可能是家庭作业,但问题很明确,提供了示例代码,并且答案也错误。 +1遵守规则。

问题很简单。 退还一磅两磅的硬币是正确的,但是这样做后您还没有完成。 然后,您应该检查下一个硬币要退还什么。 (可能是另外两磅的硬币)

乍一看,我想您需要将中断缩进到与else = 0相同的水平上。

那不是示例代码,而是我编写的代码。

您还没有使用提示:"使用整数除法和余数",并且其中有一个错字" moeny"。

Rup谢谢你,我做完了以后

是的,我不知道该怎么用。

由于您是编码的新手,因此应该开始编写在纸上执行的过程,然后找出可用于自动化该过程的工具。

好。

Important

Read the full answer in order!

Don't fall for the temptation of reading the code right away.

Ok.

The solutions I provide are hidden, but you can read them hovering your mouse over them or clicking on them (if you're using StackExchange mobile app, touch the"spoiler" link in each block).

Ok.

算法

我要做的是:

好。

假设我有一些装有硬币的垃圾箱,每个垃圾箱都标有硬币面额。

纸箱从最大面额到最小面额进行分类,在移到下一个纸箱之前,我总是从最大面额的筐中挑选尽可能多的硬币。

在纸上写下需要计算我需要的每种面额的硬币数量的值。

从第一个纸槽(具有最高面额的纸槽)开始。

从该容器中挑选我需要的硬币数量,以免我"超调"写在纸上的数量(注意该数字可以为零)。

这可以用整数除法来完成。例如,如果您的值为700,并且bin的面额为200,则您将计算整数除法700 ÷ 200 = 3 (plus a remainder of 100)

计算我挑选的硬币总数。

删除在第5步中计算出的值,并将其余值写为"新"值。

由于您已经在步骤4中计算了整数除法,因此可以计算余数。您还可以考虑在大多数编程语言中都有一个"模数"运算符,它将立即为您提供整数除法的余数。使用上面的示例,700 mod 200 = 100读取为" 700模200为100"或"整数除法运算除以700÷200的余数为100"。

移至下一个硬币箱。

从第4步开始重复,直到我使用了所有垃圾箱或该值为零。

假设我从292值开始,并且我有以下面额的容器(已经从最高面额到最低面额排序):

好。

|  200 |  100 |   50 |   20 |   10 |    5 |    2 |    1 |

+------+------+------+------+------+------+------+------+

|   I  |   II |  III |   IV |    V |   VI |  VII | VIII |

因此,让我们看看如果应用上述算法会发生什么:

好。

Write the value:   292

Start with the first bin (denomination: 200)

Pick 1 coin from the bin

The total amount picked from the bin is 200

The remainder is 92

Strike the previous value

The new value is 92

Move to the next bin (denomination: 100)

Pick 0 coins from the bin

The total amount picked from the bin is 0

The remainder is 92

Strike the previous value

The new value is 92

Move to the next bin (denomination: 50)

Pick 1 coin from the bin

The total amount picked from the bin is 50

The remainder is 42

Move to the next bin (denomination: 20)

Pick 2 coins from the bin

The total amount picked from the bin is 20

The remainder is 2

Move to the next bin (denomination: 10)

Pick 0 coins from the bin

The total amount picked from the bin is 0

The remainder is 2

Move to the next bin (denomination: 10)

Pick 0 coin from the bin

The total amount picked from the bin is 0

The remainder is 2

Move to the next bin (denomination: 5)

Pick 0 coin from the bin

The total amount picked from the bin is 0

The remainder is 2

Move to the next bin (denomination: 2)

Pick 1 coin from the bin

The total amount picked from the bin is 2

The remainder is 0

Done

用Python实现

Python是一种非常清晰的语言,可简化此类任务。因此,让我们尝试将我们的算法转换为Python。

好。

工具箱

假设您使用的是Python 3.x,则需要了解一些运算符:

好。

整数除法运算符(//):如果仅用一个斜杠除,将得到"实数除法"(例如3 / 2 == 1.5),但是如果使用双斜杠,则将得到"整数"除法(例如3 // 2 = 1)

取模运算符(%):如上所述,该运算符返回除法的余数(例如7 % 4 == 3)

好。

这些运算符一起使用,将为您提供每一步所需的信息:

好。

292 // 200 == 2

292 % 200 == 92

92 // 100 == 0

92 % 100 == 92

...

Python的一个有用特性是您可以执行"多重分配":您可以在一个步骤中将多个值分配给多个变量:

好。

# Initialize the value:

value = 292

# Initialize the denomination:

denomination = 200

# Calculate the amount of coins needed for the specified denomination

# and get the remainder (overwriting the value), in one single step:

coins, value = value // denomination, value % denomination

#              ^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^

#              |                      The remainder

#              The number of coins

#              (using integer division)

有了这些知识,我们可以编写解决方案:

好。

更正您的代码

切记:阅读以上所有内容,然后再揭示以下解决方案。

好。

def selectCoins():

twopound = 200

onepound = 100

fiftyp = 50

twentyp = 20

tenp = 10

fivep = 5

twop = 2

onep = 1

a = 0

b = 0

c = 0

d = 0

e = 0

f = 0

g = 0

h = 0

money = int(input('Enter how much money you have in pence')) # Example: 292

# Calculate the number of coins needed and the remainder

# The remainder will"overwrite" the value previously held in the"money" variable

a, money = money // twopound, money % twopound # a = 1, money = 92

b, money = money // onepound, money % onepound # b = 0, money = 92

c, money = money // fiftyp,   money % fiftyp   # c = 1, money = 42

d, money = money // twentyp,  money % twentyp  # d = 2, money = 2

e, money = money // tenp,     money % tenp     # e = 0, money = 2

f, money = money // fivep,    money % fivep    # f = 0, money = 2

g, money = money // twop,     money % twop     # g = 1, money = 0

e, money = money // onep,     money % onep     # e = 0, money = 0

print(a,b,c,d,e,f,g,h)

该解决方案使用整数除法和余数来执行计算。

好。

blockquote>

让我们以正确的方式来做:循环

让我们面对现实:上面的代码是冗长的。一定有更好的方法……而且有!使用循环。

考虑一下算法:重复执行从一个容器到下一个容器的步骤,并获取所需的硬币数量和剩余的硬币数量。这可以写成一个循环。

因此,让我们在工具箱中添加一个list:

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

然后将每个步骤的结果存储在第二个列表中:

coins = [] # We'll use the '.append()' method to add elements to this list

因此,从第一个" bin"开始:

n, money = money // denominations[0] , money % denominations[0]

coins.append(n)

让我们将其循环:

def select_coins_v2():

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

coins = []

money = int(input('Enter how much money you have in pence'))

for i in range(len(denominations)):

n, money = money // denominations[i], money % denominations[i]

coins.append(n)

print(coins)

就是这样!

好。

blockquote>

另一个改进:只获得一次面额两次使用

请注意,上面的代码仍然有问题:您两次阅读denominations。如果面额值只能读取一次,那就太好了。

当然,有一种方法:

def select_coins_v3():

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

coins = []

money = int(input('Enter how much money you have in pence'))

for d in denominations:  # 'd' will hold the value of the denomination

n, money = money // d, money % d

coins.append(n)

print(coins)

正如我的一个朋友所说:"快速,准确,简洁;不要缓慢,分散和混乱"

好。

blockquote>

TL; DR

在Python 3.x中,"整数除法"运算符为//,余数(模)运算符为%。

您可以在一行代码中执行多个分配:

a, b = 1, 2

您可以将面额存储在列表中:

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

您可以从面额列表中读取信息,并在单个步骤中获得整数除法和余数:

n, money = money // denominations[0], money % denominations[0]

您可以编写一个执行上述所有操作的循环:

for d in denominations: n, money = money // d, money % d

好。

奖励:使用字典

如果我想同时打印面额和使用的每种面额的硬币数量怎么办?您可以使用循环遍历两个列表,但是也可以使用字典来简化它:

def select_coins_v4():

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

coins = []

money = int(input('Enter how much money you have in pence'))

for d in denominations:  # 'd' will hold the value of the denomination

n, money = money // d, money % d

coins.append(n)

number_of_coins = dict(zip(denominations, coins))

print(number_of_coins)

好。

blockquote>

Python提供了很大的灵活性。随意尝试各种获取所需内容的方式...,然后选择较简单的一种。

好。

希望这可以帮助。

好。

好。

我非常感谢您从skretch到完整的解决方案(包括用于示例的工具(操作员))解释问题的方式。只是一个校正..292 // 200将是1而不是2 ....

@ShivamSeth乐于帮助!在某一点上,我们所有人都遇到了这样的麻烦...我喜欢为所有帮助过我的人付出代价

太感谢您了,这是很大的帮助。当我变得更好时,我也会付钱。

@Nab Im很高兴您发现此答案很有帮助。我希望这可以为您提供解决方案的良好指导。很快您就会发现自己可以帮助其他人解决他们的问题。都是在这里帮助

@Nab我建议您看看官方的Python教程

关于使用实际面额的最酷的事情是贪婪的解决方案将始终找到最佳解决方案...对于奇怪的面额而言,这不再成立...但是,如果将它们分成几部分,这些问题总是最容易的

def get_one_change(amt_due):

# find how many of the largest denomination that you can use is

# ie for 60 = 1x50p is the count and number of largest

# for 4 = 4x1p ; 21 = 2x10p ; etc

return pence,count # ie 50,1 would be 1 50p coin

一旦有了这个,您只需要反复调用它并调整结果,直到没有必要更改为止

def get_change(amount_due):

changes_due = [] # to keep track

while amount_due:

pence,qty_of_coin = get_one_change(amount_due)

changes_due.append({"coin":pence,"qty":qty_of_coin})

amount_due = amount_due - (pence*qty_of_coin)

return changes_due

现在您可以使用用户输入来调用get_change方法

我有点理解您告诉我的第一部分,但后来我感到困惑。我是编码新手,所以这可能是原因。

您需要实现get_one_change

@Nab提示:在python中搜索整数除法和模。那会给你你所需要的

python整钱兑换零钱while语句_关于python:如何将钱(便士)转换为单个硬币?相关推荐

  1. 导入python标准数学函数模块的语句_《Python编程快速上手——让繁琐工作自动化》——2.8 导入模块...

    本节书摘来自异步社区<Python编程快速上手--让繁琐工作自动化>一书中的第2章,第2.8节,作者[美] Al Sweigart,王海鹏 译,更多章节内容可以访问云栖社区"异步 ...

  2. python对excel增删改查语句_利用python模拟sql语句对员工表格进行增删改查

    本文主要给大家介绍了关于python模拟sql语句对员工表格进行增删改查的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 具体需求: 员工信息表程序,实现增删改查操作: 可进行模糊查询, ...

  3. python中有这样一条语句_在Python中一行书写两条语句时,语句之间可以使用__________作为分隔符。_学小易找答案...

    [填空题]Python表达式4.5/2.4.5//2和4.5%2的值分别为__________________________. [填空题]我国古代文献中所记载的最早的学校类型有两种,分别是 和 . ...

  4. python控制语句中的条件语句_『Python』条件控制语句

    Loading... ## 1. 条件语句 ``` Python条件语句是通过一条或者多条语句的执行结果(True或False)来决定执行的代码块. ``` ``` 在Python中, 指定任何非0和 ...

  5. python中try语句_[转]python 里面 try语句

    python的try语句有两种风格 一:种是处理异常(try/except/else) 二:种是无论是否发生异常都将执行最后的代码(try/finally) try/except/else风格 try ...

  6. python有几种循环语句_[14] Python循环语句(一)

    1. 概述 今天我们介绍循环语句,和条件判断一样,我们从流程图开始看起.首先看一下学习计划列表,粗体为已学,斜体为新增或修改内容.计算机编程的原理简要介绍 集成开发环境PyCharm 变量名.数字.字 ...

  7. pythonturtle简易绘图有循环语句和分支语句_使用Python的turtle模块画图的方法

    简介:turtle是一个简单的绘图工具.它提供了一个海龟,你可以把它理解为一个机器人,只听得懂有限的指令. 1.在文件头写上如下行,这能让我们在语句中插入中文 #-*-coding:utf-8-*- ...

  8. python execjs是如何请求网页的_在Python中如何执行ExecJs语句

    这次给大家带来在Python中如何执行ExecJs语句,在Python中执行ExecJs语句的注意事项有哪些,下面就是实战案例,一起来看一下. execjs模块 在网页数据提取的日常中,经常有一些有用 ...

  9. python查询和替换一个文本字符串_【Python】python面试题

    一些Python面试题 1. (1)python下多线程的限制以及多进程中传递参数的方式 python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只 ...

最新文章

  1. 快速加载生成nlp数据
  2. EOS 执行合约报错, CODE: 3090003
  3. Sqlite表的结构修改
  4. 数据网络卡顿怎么处理_监控网络卡顿怎么办
  5. JDK环境下利用记事本对java文件进行运行编译
  6. php开启与关闭错误提示
  7. mysql5.5安装最后一步一直无反应_【MATLAB R2019b】保姆级安装教程
  8. 徐波 博士 计算机,徐波教授:医工联合促进智能肿瘤学发展——探秘肿瘤精准治疗中的AI技术...
  9. 2019.01.28【NOIP普及组】模拟赛C组总结
  10. linux下c/c++实例之十socket简单应用
  11. spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config
  12. python导入gif_Python之GIF图倒放,沙雕快乐源泉!我已经笑了一天了!
  13. 上传文件和提交textfield_0基础掌握Django框架(37)文件上传
  14. TensorFlow相关
  15. 快速从mysqldump文件中恢复一个表
  16. unity3d 脚本学习系列
  17. 计算机电路基础电压比较器电路图,计算机电路基础期末复习.doc
  18. cfe刷机教程 斐讯k3_2019斐讯K3全版本免拆机免降级刷机教程
  19. soundpool android,Android中用SoundPool播放音频
  20. 创新电影院布局5G+4K,移动电影院成放映领域的“黑科技”

热门文章

  1. 如何制作带参数的批处理文件
  2. Acrobat 9 Pro安装教程
  3. kali dpkg无法访问归档
  4. 计算机等级考试是大学教的吗,计算机二级是大学生必考的吗
  5. 应用之星移动应用快速开发平台概述
  6. 应用之星带你玩转H5页面和app开发,不懂技术的看过来
  7. 逗比表单之http的学习(附别的小实验)
  8. MacOs平台下 Vs2022 for Mac、Xamarin、IOS Android 双平台证书申请、开发环境配置、实机测试、内部分发B(贝塔)测试、 App store发布 超详细(多图)全程笔记
  9. 建筑涂料消泡剂消泡实力引争议?
  10. 我来挑战主页绑定,浏览器被绑架之终极方案!