• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一泽涟漪
时光荏苒 白驹过隙
博客园    首页    新随笔    联系   管理    订阅  订阅
python while循环

循环打印1~5

i = 1
while i <= 5:
    print(i)
    i += 1

输出

1
2
3
4
5
View Code

循环执行判断用户输入,若输入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
View Code

 使用循环标志

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
View Code

使用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===================
posted on 2019-12-13 11:03  一泽涟漪  阅读(465)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3