Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回顺序输出的计数函数_Python_Printing_Count_Return_Output - Fatal编程技术网

Python 返回顺序输出的计数函数

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

是否有这样一个函数,每次调用关键字或操作时,它都会触发一个返回或计数,以更改为可以分配输出的值 这意味着根据您说“喝一杯”的次数,输出将发生变化,直到(在以下情况下)其“全部为空:

如果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")

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。