我想写一个程序来模拟自动售货机,并根据支付的金额计算出零钱(必须退还给你)。考虑到成本,首先应该提示用户添加更多的钱,直到支付满足/超过成本。在
假设所有的零钱都是用硬币换的,硬币的面额是: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
当我运行此程序并按照说明操作时,它应该如下所示:
^{pr2}$相反,我得到:
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
其余的都是正确的。在
谢谢你们所有的帮手(尤其是@jornsharpe)。以下是解决方案(以代码形式):
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
硬币,而不是猜测他们将输入什么。在以下是一种可能的实现方式:
请注意这两个函数之间的职责划分,以便于分别测试每个函数,并适当使用}循环以尽量减少重复。另外,我已经使
for
和{amounts
和paid
依赖于coins
,所以你只需要在一个地方改变就可以添加新的硬币。在用法示例:
^{pr2}$相关问题 更多 >
编程相关推荐