zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    vue数组和对象不能直接赋值
    数组内部的对象监听问题
    vue知识点(工作中遇到)
    Array数组
    js对象操作
    Vue-router
    sublime text 3 常用插件 —— SFTP
    Python中的线程和进程
    python中的sockeserver模块简单实用
    python利用socket写一个文件上传
  • 原文地址:https://www.cnblogs.com/ilifeilong/p/12033989.html
Copyright © 2011-2022 走看看