Python 返回顺序输出的计数函数
是否有这样一个函数,每次调用关键字或操作时,它都会触发一个返回或计数,以更改为可以分配输出的值 这意味着根据您说“喝一杯”的次数,输出将发生变化,直到(在以下情况下)其“全部为空: 如果action=“喝一杯” 行动:喝一杯Python 返回顺序输出的计数函数,python,printing,count,return,output,Python,Printing,Count,Return,Output,是否有这样一个函数,每次调用关键字或操作时,它都会触发一个返回或计数,以更改为可以分配输出的值 这意味着根据您说“喝一杯”的次数,输出将发生变化,直到(在以下情况下)其“全部为空: 如果action=“喝一杯” 行动:喝一杯 add 1 to count.drink count 0 = print("full") count 1 = print ("3/4 left") count 2 = print("1/2 left") count 3 = print ("1/4 left") c
add 1 to count.drink
count 0 = print("full")
count 1 = print ("3/4 left")
count 2 = print("1/2 left")
count 3 = print ("1/4 left")
count 4 = print("all empty")
count = 1
reaction "3/4 left"
count = 2
reaction "1/2 left"
count = 3
reaction "1/4 left"
count = 4
reaction "all empty"
行动:喝一杯
add 1 to count.drink
count 0 = print("full")
count 1 = print ("3/4 left")
count 2 = print("1/2 left")
count 3 = print ("1/4 left")
count 4 = print("all empty")
count = 1
reaction "3/4 left"
count = 2
reaction "1/2 left"
count = 3
reaction "1/4 left"
count = 4
reaction "all empty"
行动:喝一杯
add 1 to count.drink
count 0 = print("full")
count 1 = print ("3/4 left")
count 2 = print("1/2 left")
count 3 = print ("1/4 left")
count 4 = print("all empty")
count = 1
reaction "3/4 left"
count = 2
reaction "1/2 left"
count = 3
reaction "1/4 left"
count = 4
reaction "all empty"
行动:喝一杯
add 1 to count.drink
count 0 = print("full")
count 1 = print ("3/4 left")
count 2 = print("1/2 left")
count 3 = print ("1/4 left")
count 4 = print("all empty")
count = 1
reaction "3/4 left"
count = 2
reaction "1/2 left"
count = 3
reaction "1/4 left"
count = 4
reaction "all empty"
在我的评论之后,我将提出一个解决方案
actDictCount = {"take a drink":0}
actDictList = {"take a drink":["action1","action2","action3"]}
action = "take a drink"
actDictCount[action] +=1
print(actDictList[action][actDictCount[action]])
我建议使用Dictionary来解决这个问题。python中的Dictionary可以调用函数/方法。您可以将“take a drink”设置为键,并增加dict。