Conver 4.5欧元兑换欧元50美分20美分10美分等 [英] Conver 4.5 euro change into the amount of euros 50 cents 20 cents 10 cents etc

查看:140
本文介绍了Conver 4.5欧元兑换欧元50美分20美分10美分等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道其他方法如何做到这一点我想找到一个更简单的方法这是Python

我尝试过并试过但是没有人能帮助我吗?



两个输入值,项目价格和投标金额,以美分输入,例如3580€35.80和4000€40.00€

以下列格式显示输出:



购买价格:€35.80

招标金额:€40.00



您的更改为:€4.20

4欧元硬币

0 50分硬币

1 20美分硬币

0 10美分硬币

0 5美分硬币

0美分



这就是我的尝试



我尝试了什么:



 EURO =  100  
purchase_price = int(输入( 输入价格))

amount_tendered = int(输入( 输入招标金额))

your_change =(amount_tendered - purchase_price)

如果 amount_tendered> = purchase_price:

one_eu ro = your_change // EURO
fifty_cents = your_change%50
twenty_cents = your_change%20

print (one_euro)
print (fifty_cents)
print (twenty_cents)
else
print < span class =code-string> STOP)

解决方案

引用:

4欧元硬币

0 50美分硬币

1 20美分硬币

0 10分硬币

0 5美分硬币

0美分



缺少2美分硬币。



您知道如何更改任何给定金额。

您的代码不做的是每次给硬币计算剩余的变化。

当你给'4欧元硬币'时,你需要从 your_change 中推断出来。



[更新]

引用:

所以我做错了就是我没有对所有人做同样的事情,例如

one_euro = your_change // EURO

fifty_cents =(your_change - (100 * one_euro))// 50

twenty_cents =(your_change - (100 * one_euro))// 20



更好,但仍然是错误的。

尝试更改4.88,4.49并尝试其他值,直到您确信它有效。



在现实生活中,当你进行改变时,你还必须处理你拥有的每个值的硬币数量。


整数除法和模数(和列表)工作奇迹。

 purchase_price = int(输入( 输入价格))

amount_tendered = int(输入( 输入金额趋向ered))

your_change =(amount_tendered - purchase_price)

if your_change< 0
退出( 1

remaining_change = your_change

coins = [ 100 50 20 10 5 ]
names = [' euro'' 五十美分' 二十美分' 十美分' 五美分]

index = 0
for c in 币:
count = remaining_change // c
remaining_change = remaining_change%c
if 计数> 0
print %d%s%(count,names [index]))
index = index + 1 < /跨度>


简单。你没有考虑过你以前用过的金额。

因此,如果你使用1分50美分,你需要从你必须给予的金额中扣除这个价值 - 50美分。 br />


并且......如果一切都以美分为单位,为什么要将其转换为浮点欧元,然后将其除以每个硬币的分数?留下美分,这一切都变得容易多了。


I know other methods on how to do it bu i want to find a simpler one this is Python
I tried and tried but it wouldn't come right anyone can help me ?

The two input values, item price and amount tendered, are entered in cents, e.g. 3580 for €35.80 and 4000 for €40.00.
Display the output in the following format:

Purchase Price: €35.80
Amount Tendered: €40.00

Your change is: €4.20
4 one Euro coins
0 50 cent coins
1 20 cent coins
0 10 cent coins
0 5 cent coins
0 cents

This is what i tried

What I have tried:

EURO = 100
purchase_price = int(input("Enter The Price "))

amount_tendered = int(input("Enter The Amount Tendered"))

your_change = (amount_tendered - purchase_price)

if amount_tendered >= purchase_price:

  one_euro = your_change//EURO
  fifty_cents = your_change%50
  twenty_cents = your_change%20

  print(one_euro)
  print(fifty_cents)
  print(twenty_cents)
else:
    print("STOP")

解决方案

Quote:

4 one Euro coins
0 50 cent coins
1 20 cent coins
0 10 cent coins
0 5 cent coins
0 cents


The '2 cent coins' are missing.

You know how the make change for any given amount.
What you code don't do is calculating the remaining of change every time you give coins.
When you give '4 one Euro coins', you need to deduce them from your_change.

[Update]

Quote:

So what i was doing wrong is I was not doing the same for all of them e.g.
one_euro = your_change//EURO
fifty_cents = (your_change -(100*one_euro))//50
twenty_cents =(your_change -(100*one_euro))//20


Better, but still wrong.
Try with a change of 4.88, 4.49 and try other values until you are confident that it works.

In real life, when you give change, you also have to deal with how many coins of each values you have.


Integer division and modulus (and lists) work miracles.

purchase_price = int(input("Enter The Price "))

amount_tendered = int(input("Enter The Amount Tendered "))

your_change = (amount_tendered - purchase_price)

if  your_change < 0 :
  exit(1)

remain_change = your_change

coins = [100, 50, 20 , 10, 5]
names = ['euro', 'fifty cents', 'twenty cents', 'ten cents', 'five cents']

index = 0
for c in coins:
  count = remain_change // c
  remain_change = remain_change % c
  if count > 0:
    print( "%d %s" % (count, names[index]))
  index = index +1


Simple. YOu aren't accounting for the amount you have used previously.
So if you use 1 50 cent, you need to deduct that value - 50 cents - from the amount you have to give them back.

And ... if everything is in terms of cents, why are you converting it to floating point Euros, and then dividing that by the number of cents in each coin? Leave it in cents, and it all becomes a lot easier.


这篇关于Conver 4.5欧元兑换欧元50美分20美分10美分等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆