Python:如何在获得特定输入之前继续重复程序?

 天之苍 发布于 2023-02-13 13:00

我有一个评估输入的函数,我需要继续询问他们的输入并进行评估,直到他们输入一个空行.我怎么设置它?

while input != '':
    evaluate input

我想过用这样的东西,但它并没有完全奏效.有帮助吗?

1 个回答
  • 有两种方法可以做到这一点.首先是这样的:

    while True:             # Loop continuously
        inp = raw_input()   # Get the input
        if inp == "":       # If it is a blank line...
            break           # ...break the loop
    

    第二个是这样的:

    inp = raw_input()       # Get the input
    while inp != "":        # Loop until it is a blank line
        inp = raw_input()   # Get the input again
    

    请注意,如果您使用的是Python 3.x,则需要替换raw_inputinput.

    2023-02-13 13:02 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有