循环打印1~5
i = 1 while i <= 5: print(i) i += 1
输出
1 2 3 4 5
循环执行判断用户输入,若输入quit就退出循环
prompt = "Please enter 'quit' to logoff program: " message = "" while message != 'quit': message = input(prompt) print(message)
输出
Please enter 'quit' to logoff program: exit exit Please enter 'quit' to logoff program: byebye byebye Please enter 'quit' to logoff program: quit quit
使用循环标志
active = True prompt = "Please enter 'quit' to logoff program: " message = "" while active: if message != 'quit': message = input(prompt) print(message) else: active = False
输出
Please enter 'quit' to logoff program: byebye byebye Please enter 'quit' to logoff program: exit exit Please enter 'quit' to logoff program: quit quit
使用break退出循环
prompt = "Please enter 'quit' to logoff program: " message = "" while True: if message != 'quit': message = input(prompt) print(message) else: break
输出
Please enter 'quit' to logoff program: bye bye Please enter 'quit' to logoff program: exit exit Please enter 'quit' to logoff program: quit quit
===================来自一泽涟漪的博客,转载请标明出处 www.cnblogs.com/ilifeilong===================