''' Welcome to LearnPython.NET File Name: PLoops.py Download from:https://www.learnpython.net/cn/python-code-samples.html Author: LearnPython.Net Editor: CoderChiu ''' #抓到的螃蟹数量; nCrabNumAry = [2, 3, 5, 7] #总计螃蟹数量; nCrabSum = 0 #统计总的螃蟹数量; for Crabs in nCrabNumAry: nCrabSum = nCrabSum + Crabs # print("Catch crabs:", nCrabSum) #统计螃蟹数量 nLen = len(nCrabNumAry) #重置为0,开始计数 nCrabSum = 0 #索引; nIndex = 0 while nIndex < nLen: nCrabSum = nCrabSum + nCrabNumAry[nIndex] nIndex = nIndex + 1 print("Catch crabs:", nCrabSum)