我尝试进行具有序列选择的自动响应。
choice = ['welcome','welcome back','nice to see you','hai dude']
>>> welcome
>>> welcome back
>>> nice to see you
>>> hai dude
>>> welcome
>>> welcome back
>>> nice to see you
>>> hai dude
在我使用random.choice()之前,如何从“欢迎”直到“hai dude”进行序列选择并一次又一次地回到“walcome”,但是现在我需要序列选择,任何人都可以给我建议???
答案 0 :(得分:4)
您可以导入from itertools import cycle
In [3]: c=cycle(['welcome','welcome back','nice to see you','hai dude'] )
In [4]: next(c)
Out[4]: 'welcome'
In [5]: next(c)
Out[5]: 'welcome back'
In [6]: next(c)
Out[6]: 'nice to see you'
In [7]: next(c)
Out[7]: 'hai dude'
In [8]: next(c)
Out[8]: 'welcome'
In [9]: next(c)
Out[9]: 'welcome back'
<强>更新强>
from itertools import cycle
c=cycle(['welcome','welcome back','nice to see you','hai dude'] )
print next(c)
print next(c)
next(c)
会不断提供下一个元素。