简体   繁体   中英

python variable reset inside loop

x=0
while x <= 5:
    i = 0
    while i <= 5:
        print("J", end=" ")
        i+=1
    x+=1
    print()

Can anyone explain me why the var i get reset after while loop. Isn't supposed to be remained like the var x ?

x=0
while x <= 5:
    i = 0
    while i <= 5:
        print("J", end=" ")
        i+=1
    x+=1
    print(i)

Yes, variable i will be equal to zero because at the start of your outer while loop, you have i = 0 . So that is why.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM