[英]How to return the value of a while loop counter
我想在每次循環重復后返回 while 循環的計數器 i 和 b 以用於另一個 function。我沒有發現任何與返回這些值相關的內容。
def dis(reps, towards, back):
t = 0 # move towards t times
b = 0 # move back b times
i = 0 # repetitions
n = 1 # counts every step
while i < reps:
while t < towards:
do_something()
i += 1
n +=1
while b < back:
do_something()
n += 1
b += 1
t = 0
b = 0
i += 1
我想添加另一個變量,如 counter_towards 和 counter_back 並每次添加一個值,但這不能解決我的問題,因為我仍然必須在每次代表時返回這些值。 將相關功能添加到 class 可能會工作,但這需要大量工作,我認為這個問題可能有一個簡單的答案。
您必須觀看 python 發電機。 這是一個鏈接!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.