自动售货机程序(计算需投入金额等)
我想写一个程序来模拟自动售货机,并根据支付的金额计算找回的零钱。用户在输入费用后,应该首先被提示继续投币,直到支付的金额达到或超过费用。
假设所有的找零都是用硬币来找的,硬币的面值有:1分、5分、10分、25分和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()
1 个回答
2
你遇到的具体错误是因为你没有正确处理当金额刚好达到总数的情况——你超出了目标,然后还得找零。不过,你的代码很长也很复杂,想要搞清楚每一步到底在做什么有点困难。
这里有一些通用的编码建议:
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'