我想写一个程序来模拟自动售货机,并根据支付的金额计算零钱(必须退还给您)。考虑到成本,应该首先提示用户添加更多的钱,直到支付达到/超过成本。
假设所有零钱仅以硬币形式提供,硬币有以下面额:1c、5c、10c、25c、$1
这是我的程序:
x = eval(input("Enter the cost (in cents):n"))
b = 0
for i in range(x+500):
if x<5 and x>=b:
b += 1
print("Deposit a coin or note (in cents):")
print(1)
diff = b-x
for i in range(diff):
onecents = diff//1
new_onecents = diff - (onecents*1)
print("Your change is:")
if onecents != 0:
print(onecents,"x 1c")
break
elif x<10 and x>=b:
b += 5
print("Deposit a coin or note (in cents):")
print(5)
diff = b-x
for i in range(diff):
fivecents = diff//5
new_fivecents = diff - (fivecents*5)
onecents = new_fivecents//1
new_onecents = new_fivecents - (onecents*1)
print("Your change is:")
if fivecents != 0:
print(fivecents,"x 5c")
if onecents != 0:
print(onecents,"x 1c")
break
elif x<25 and x>=b:
b += 10
print("Deposit a coin or note (in cents):")
print(10)
diff = b-x
for i in range(diff):
tencents = diff//10
new_tencents = diff - (tencents*10)
fivecents = new_tencents//5
new_fivecents = new_tencents - (fivecents*5)
onecents = new_fivecents//1
new_onecents = new_fivecents - (onecents*1)
print("Your change is:")
if tencents !=0:
print(tencents,"x 10c")
if fivecents != 0:
print(fivecents,"x 5c")
if onecents != 0:
print(onecents,"x 1c")
break
elif x<100 and x>=b:
b += 25
print("Deposit a coin or note (in cents):")
print(25)
diff= b-x
for i in range(diff):
quarters = diff//25
new_quarters = diff - (quarters*25)
tencents = new_quarters//10
new_tencents = new_quarters - (tencents*10)
fivecents = new_tencents//5
new_fivecents = new_tencents - (fivecents*5)
onecents = new_fivecents//1
new_onecents = new_fivecents - (onecents*1)
print("Your change is:")
if quarters !=0:
print(quarters,"x 25c")
if tencents !=0:
print(tencents,"x 10c")
if fivecents != 0:
print(fivecents,"x 5c")
if onecents != 0:
print(onecents,"x 1c")
break
elif x<500 and x>b:
print("Deposit a coin or note (in cents):")
print(100)
b += 100
diff = b-x
for i in range(diff):
quarters = diff//25
new_quarters = diff - (quarters*25)
tencents = new_quarters//10
new_tencents = new_quarters - (tencents*10)
fivecents = new_tencents//5
new_fivecents = new_tencents - (fivecents*5)
onecents = new_fivecents//1
new_onecents = new_fivecents - (onecents*1)
print("Your change is:")
if quarters !=0:
print(quarters,"x 25c")
if tencents !=0:
print(tencents,"x 10c")
if fivecents != 0:
print(fivecents,"x 5c")
if onecents != 0:
print(onecents,"x 1c")
break
elif x<(x+500) and x>=b:
print("Deposit a coin or note (in cents):")
print(500)
b += 500
diff = b-x
for i in range(diff):
onedollars = diff//100
new_onedollars = diff - (onedollars * 100)
quarters = new_onedollars//25
new_quarters = new_onedollars - (quarters*25)
tencents = new_quarters//10
new_tencents = new_quarters - (tencents*10)
fivecents = new_tencents//5
new_fivecents = new_tencents - (fivecents*5)
onecents = new_fivecents//1
new_onecents = new_fivecents - (onecents*1)
print("Your change is:")
if onedollars != 0:
print(onedollars,"x $1")
if quarters !=0:
print(quarters,"x 25c")
if tencents !=0:
print(tencents,"x 10c")
if fivecents != 0:
print(fivecents,"x 5c")
if onecents != 0:
print(onecents,"x 1c")
break
当我运行这个程序并按照说明操作时,它应该是这样的:
Enter the cost (in cents):
1000
Deposit a coin or note (in cents):
500
Deposit a coin or note (in cents):
500
Deposit a coin or note (in cents):
相反,我得到了:
Enter the cost (in cents):
1000
Deposit a coin or note (in cents):
500
Deposit a coin or note (in cents):
500
Deposit a coin or note (in cents):
500
Your change is:
5 x $1
还有另一个预期输出:
Enter the cost (in cents):
3
Deposit a coin or note (in cents):
1
Deposit a coin or note (in cents):
1
Deposit a coin or note (in cents):
1
然而我得到:
Enter the cost (in cents):
3
Deposit a coin or note (in cents):
1
Deposit a coin or note (in cents):
1
Deposit a coin or note (in cents):
1
Deposit a coin or note (in cents):
1
Your change is:
1 x 1c
其余的都是正确的。
谢谢你们所有的帮助(尤其是@jonrsharpe)。以下是解决方案(以代码形式):
def vend():
"""Simulate a vending machine, taking user input and returning remainder."""
total = eval(input("Enter the cost (in cents):n"))
inserted = 0
while inserted < total:
inserted += eval(input("Deposit a coin or note (in cents):n"))
if inserted > total:
sum = inserted - total
if sum != 0:
print("Your change is:")
dollars = sum//100
if dollars != 0:
print(dollars,'x $1')
quarters = (sum - dollars*100)//25
if quarters != 0:
print(quarters,'x 25c')
ten_cents = (sum - dollars*100 - quarters*25)//10
if ten_cents != 0:
print(ten_cents,'x 10c')
five_cents = (sum - dollars*100 - quarters*25 - ten_cents*10)//5
if five_cents != 0:
print(five_cents,'x 5c')
one_cents = (sum - dollars*100 - quarters*25 - ten_cents*10 - five_cents*5)//1
if one_cents != 0:
print(one_cents,'x 1c')
vend()
您的特定错误源于这样一个事实,即您没有正确处理精确达到总数的情况——您超出了总数,然后必须进行更改。然而,您的代码非常长且复杂,很难准确地弄清楚它在每个阶段都在做什么。
一些通用编码建议:
eval
是个坏主意;最好使用int(input(...))
,如果用户不输入整数,这也会让你很麻烦- 您的外部
for
循环实际上应该是一个while
循环,而不是猜测最大迭代次数(即使在您做出更改之后,也都会运行!) - 您有很多重复代码和硬编码值;每当您重复编写类似的东西时,请考虑将它们拆分为带有参数的函数或某种循环
此外,阅读您的描述,特别是:
考虑到成本,应该首先提示用户添加更多的钱,直到支付达到/超过成本。
我认为你应该允许用户使用input
硬币,而不是猜测他们会输入什么。
以下是一种可能的实现方式:
def vend():
"""Simulate a vending machine, taking user input and returning remainder."""
total = int(input("Enter the cost (in cents): "))
inserted = 0
while inserted < total:
inserted += int(input("Deposit a coin or note (in cents): "))
if inserted > total:
return make_change(inserted - total)
def make_change(remainder):
"""Calculate the coins required to make change equal to amount."""
coins = ["$1", "25c", "10c", "5c", "1c"]
amounts = [int(coin[:-1]) if coin.endswith("c")
else 100 * int(coin[1:])
for coin in coins]
counts = [0 for _ in coins]
for index, amount in enumerate(amounts):
counts[index] = remainder // amount
remainder %= amount
return ", ".join("{0} x {1}".format(count, coin)
for count, coin in zip(counts, coins)
if count)
注意两个函数之间的责任划分,以便更容易分别测试每个函数,并适当使用for
和while
循环,以尽量减少重复。此外,我已经使make_change
中的amounts
和paid
依赖于coins
,所以您只需要在一个地方更改即可添加新硬币。
示例用法:
>>> vend()
Enter the cost (in cents): 135
Deposit a coin or note (in cents): 100
Deposit a coin or note (in cents): 50
'1 x 10c, 1 x 5c'