while True: msg = input('>> ').strip() print(msg)View Code
#!/usr/bin/env python # author:lijipeng hobbies = ['sing','on foot','table tennis'] index = 0 while index < len(hobbies): print(hobbies[index]) index+=1 #①:满足重复 #②:并且以每次重新赋值后的Index值作为下一次循环的索引进行取值,反复迭代 #最终可以取尽列表中所有的值View Code
#!/usr/bin/env python # author:lijipeng hobbies = ['sing','on foot','table tennis'] i = iter(hobbies) # 每次都需要重新获取一个迭代器对象 while True: try: print(next(i)) except StopIteration: # 捕捉异常,然后终止循环 break for item in hobbies: print(item) # for循环的工作原理: # ①、调用可迭代对象hobbies内置__iter__得到一个迭代器对象 # ②、调用迭代器对象内置的__next__得到一个值 # ③、将得到的值赋给item,执行循环体完成一次循环 # ④、一直到捕捉到StopIteration一场,结束迭代View Code
#!/usr/bin/env python # author:lijipeng hobbies = ['sing','on foot','table tennis'] i = iter(hobbies) while True: try: print(next(i)) except StopIteration: break # i1 = iter(hobbies) # 两个或者多个循环使用同一个迭代器,必然只会有一个循环能取到值 while True: try: print(next(i)) except StopIteration: breakView Code