该脚本一直运行到“#”;调用函数然后在它应该运行函数时停止。它不会停止任何错误。这是为什么?
# Please note that this only works in integer values, since there is no change in pence
notes = (1,5,10,20,50) #Value of notes
quantities = [10,8,5,5,1] #Quantities of notes
# Defining variables
notesout = []
total = 0
x = -1
payment = []
# This loop works out the total amount of cash in the cash register
while (x < 4):
x += 1
calc = notes[x]*quantities[x]
total += calc
mon_nd = 70 # Money needed
def takenotes():
print("Please input each notes value, when finished type \"stop\"")
# If input is an int then add to payment list, if not then work out the change
payment = [20,20,20,20]
main()
def main():
# Finds the value of the cash given
paymentV = sum(payment)
changeT = paymentV - mon_nd
# Change the quantities of the 'quantities' variable
for i in payment:
quantities[notes.index(i)] = quantities[notes.index(i)] + 1
while(changeT < 0):
# Works out what amount of change should be given
for i in reversed(notes):
if (changeT - i >= 0):
notesout.append(i)
quantities[notes.index(i)] = quantities[notes.index(i)]-1
changeT -= i
else:
return True
print(notesout)
takenotes()
答案 0 :(得分:0)
它没有&#34;只是停止&#34;。 takenotes
来电main
;它进入while循环内的for循环;第一轮,changeT - i
不大于0,因此返回True。由于您没有对main
的返回值执行任何操作,因此不打印任何内容,程序结束。
答案 1 :(得分:0)
首先,您需要global
语句来更改任何全局变量(例如payment
)。
payment = []
def takenotes():
global payment
payment = [20, 20, 20, 20]
您的代码中也没有input()
函数。请参阅the docs。
答案 2 :(得分:0)
此脚本正确运行。它调用pmotes()函数然后正常执行它(显示消息,设置本地支付数组,然后执行main()函数)。 您可以在this online Python interpreter上查看此信息。您也可以逐步执行here来查看脚本的确切功能。
此外,当您要编辑全局变量时,必须使用全局语句。阅读this SO question的答案以获取更多信息。