![](/img/trans.png)
[英]Python i would like to know how to writerow with a for loop (DictWriter)
[英]I would like to know how can I make while loop?
def check():
a = 0
b = 10
while a <= b:
print(a, b)
while a <= b:
b -=1
print(a, b)
a += 1
結果是:
0 10
0 9
0 8
0 7
0 6
0 5
0 4
0 3
0 2
0 1
0 0
0 -1
我期待下面的結果,我應該如何編輯它?
0 10
0 9
0 8
0 7
0 6
0 5
0 4
0 3
0 2
0 1
0 0
0 -1
1 10
1 9
1 8
1 7
1 6
1 5
1 4
1 3
1 2
1 1
1 0
1 -1
2 10
2 9
2 8
2 7
2 6
2 5
2 4
2 3
2 2
2 1
2 0
2 -1
3 10
3 9
3 8
3 7
3 6
3 5
3 4
3 3
3 2
3 1
3 0
3 -1
直到....10
嘗試這個:
b = 0
while b <= 10:
a = 10
while a >= -1:
print(b, a)
a -= 1
b += 1
您忘記了恢復起始值。 並且可能想要第三個變量。 為了清楚起見,添加了一些額外的變量,這樣您就可以嘗試跟蹤您的 while 循環發生了什么。
def check():
a = 0
limit = 10
b_initial = 10
b = b_initial
while(a<=limit): #We run this from a= 0 until a=limit
#b=b_initial Alternative location to reset b to initial value of b
while(b>=-1): #You want to decrease b until -1 for all values of a
print(a,b)
b-=1 # decreasing b
a+=1 #once the inner while loop is finished increase a by 1
b= b_initial # reset b to 10.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.