What does += mean in Python?

后端 未结 6 495
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 08:29

I see code like this for example in Python:

    if cnt > 0 and len(aStr) > 1:
        while cnt > 0:                  
            aStr = aStr[1:]+a         


        
6条回答
  •  抹茶落季
    2020-12-01 09:24

    FYI: it looks like you might have an infinite loop in your example...

    if cnt > 0 and len(aStr) > 1:
        while cnt > 0:                  
            aStr = aStr[1:]+aStr[0]
            cnt += 1
    
    • a condition of entering the loop is that cnt is greater than 0
    • the loop continues to run as long as cnt is greater than 0
    • each iteration of the loop increments cnt by 1

    The net result is that cnt will always be greater than 0 and the loop will never exit.

提交回复
热议问题