栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

APT命令行界面一样的是/否输入?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

APT命令行界面一样的是/否输入?

正如您提到的,最简单的方法是使用

raw_input()
(或仅
input()
对于Python
3
)。没有内置的方法可以做到这一点。从577058号配方中:

import sysdef query_yes_no(question, default="yes"):    """Ask a yes/no question via raw_input() and return their answer.    "question" is a string that is presented to the user.    "default" is the presumed answer if the user just hits <Enter>.        It must be "yes" (the default), "no" or None (meaning        an answer is required of the user).    The "answer" return value is True for "yes" or False for "no".    """    valid = {"yes": True, "y": True, "ye": True,  "no": False, "n": False}    if default is None:        prompt = " [y/n] "    elif default == "yes":        prompt = " [Y/n] "    elif default == "no":        prompt = " [y/N] "    else:        raise ValueError("invalid default answer: '%s'" % default)    while True:        sys.stdout.write(question + prompt)        choice = raw_input().lower()        if default is not None and choice == '': return valid[default]        elif choice in valid: return valid[choice]        else: sys.stdout.write("Please respond with 'yes' or 'no' "       "(or 'y' or 'n').n")

用法示例:

>>> query_yes_no("Is cabbage yummier than cauliflower?")Is cabbage yummier than cauliflower? [Y/n] oopsPlease respond with 'yes' or 'no' (or 'y' or 'n').Is cabbage yummier than cauliflower? [Y/n] [ENTER]>>> True>>> query_yes_no("Is cabbage yummier than cauliflower?", None)Is cabbage yummier than cauliflower? [y/n] [ENTER]Please respond with 'yes' or 'no' (or 'y' or 'n').Is cabbage yummier than cauliflower? [y/n] y>>> True


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号