Интерфейс командной строки APT, например, да/нет?
Есть ли какой-либо короткий способ достичь того, что делает интерфейс командной строки APT (Advanced Package Tool) в Python?
Я имею в виду, что когда диспетчер пакетов запрашивает вопрос "да/нет", за которым следует [Yes/no]
, script принимает YES/Y/yes/y
или Enter (по умолчанию Yes
как намечено заглавной буквой).
Единственное, что я нашел в официальных документах, - это input
и raw_input
...
Я знаю, что это не так сложно подражать, но это раздражает переписывать: |
Ответы
Ответ 1
Как вы упомянули, самый простой способ - это использовать raw_input()
(или просто input()
для Python 3). Нет встроенного способа сделать это. Из рецепта 577058:
import sys
def 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] oops
Please 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
Ответ 2
Я бы сделал это следующим образом:
# raw_input returns the empty string for "enter"
yes = {'yes','y', 'ye', ''}
no = {'no','n'}
choice = raw_input().lower()
if choice in yes:
return True
elif choice in no:
return False
else:
sys.stdout.write("Please respond with 'yes' or 'no'")
Ответ 3
В стандартной библиотеке Python существует функция strtobool
: http://docs.python.org/2/distutils/apiref.html?highlight=distutils.util#distutils.util.strtobool
Вы можете использовать его для проверки ввода пользователя и преобразования его в значения True
или False
.
Ответ 4
Очень простой (но не очень сложный) способ сделать это для одного выбора:
msg = 'Shall I?'
shall = input("%s (y/N) " % msg).lower() == 'y'
Вы также можете написать простую (слегка улучшенную) функцию:
def yn_choice(message, default='y'):
choices = 'Y/n' if default.lower() in ('y', 'yes') else 'y/N'
choice = input("%s (%s) " % (message, choices))
values = ('y', 'yes', '') if choices == 'Y/n' else ('y', 'yes')
return choice.strip().lower() in values
Примечание. В Python 2 используйте raw_input
вместо input
.
Ответ 5
как упомянуто @Александром Артеменко, здесь простое решение с использованием strtobool
from distutils.util import strtobool
def user_yes_no_query(question):
sys.stdout.write('%s [y/n]\n' % question)
while True:
try:
return strtobool(raw_input().lower())
except ValueError:
sys.stdout.write('Please respond with \'y\' or \'n\'.\n')
#usage
>>> user_yes_no_query('Do you like cheese?')
Do you like cheese? [y/n]
Only on tuesdays
Please respond with 'y' or 'n'.
ok
Please respond with 'y' or 'n'.
y
>>> True
Ответ 6
Вы можете использовать метод click confirm
.
import click
if click.confirm('Do you want to continue?', default=True):
print('Do something')
Это напечатает:
$ Do you want to continue? [Y/n]:
Работает на Python 2/3
на Linux, Mac или Windows.
Документы: http://click.pocoo.org/5/prompts/#confirmation-prompts
Ответ 7
Я знаю, что на это ответили кучу способов, и это может не отвечать на конкретный вопрос OP (со списком критериев), но это то, что я сделал для наиболее распространенного варианта использования, и это намного проще, чем другие ответы:
answer = input('Please indicate approval: [y/n]')
if not answer or answer[0].lower() != 'y':
print('You did not indicate approval')
exit(1)
Ответ 8
Вы также можете использовать prompter.
Бесстыдно взято из README:
#pip install prompter
from prompter import yesno
>>> yesno('Really?')
Really? [Y/n]
True
>>> yesno('Really?')
Really? [Y/n] no
False
>>> yesno('Really?', default='no')
Really? [y/N]
True
Ответ 9
Я изменил ответ fmark на python 2/3, более совместимый с python.
Смотрите служебный модуль ipython, если вы заинтересованы в чем-то с большей обработкой ошибок
# PY2/3 compatibility
from __future__ import print_function
# You could use the six package for this
try:
input_ = raw_input
except NameError:
input_ = input
def query_yes_no(question, default=True):
"""Ask a yes/no question via standard input and return the answer.
If invalid input is given, the user will be asked until
they acutally give valid input.
Args:
question(str):
A question that is presented to the user.
default(bool|None):
The default value when enter is pressed with no value.
When None, there is no default value and the query
will loop.
Returns:
A bool indicating whether user has entered yes or no.
Side Effects:
Blocks program execution until valid input(y/n) is given.
"""
yes_list = ["yes", "y"]
no_list = ["no", "n"]
default_dict = { # default => prompt default string
None: "[y/n]",
True: "[Y/n]",
False: "[y/N]",
}
default_str = default_dict[default]
prompt_str = "%s %s " % (question, default_str)
while True:
choice = input_(prompt_str).lower()
if not choice and default is not None:
return default
if choice in yes_list:
return True
if choice in no_list:
return False
notification_str = "Please respond with 'y' or 'n'"
print(notification_str)
Ответ 10
на 2.7, является ли это слишком непитоновым?
if raw_input('your prompt').lower()[0]=='y':
your code here
else:
alternate code here
он фиксирует любое изменение Да как минимум.
Ответ 11
Выполнение этого же с python 3.x, где raw_input()
не существует:
def ask(question, default = None):
hasDefault = default is not None
prompt = (question
+ " [" + ["y", "Y"][hasDefault and default] + "/"
+ ["n", "N"][hasDefault and not default] + "] ")
while True:
sys.stdout.write(prompt)
choice = input().strip().lower()
if choice == '':
if default is not None:
return default
else:
if "yes".startswith(choice):
return True
if "no".startswith(choice):
return False
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
Ответ 12
Вы можете попробовать что-то вроде приведенного ниже кода, чтобы иметь возможность работать с выбором из предлагаемого здесь варианта: "
print( 'accepted: {}'.format(accepted) )
# accepted: {'yes': ['', 'Yes', 'yes', 'YES', 'y', 'Y'], 'no': ['No', 'no', 'NO', 'n', 'N']}
Вот код.
#!/usr/bin/python3
def makeChoi(yeh, neh):
accept = {}
# for w in words:
accept['yes'] = [ '', yeh, yeh.lower(), yeh.upper(), yeh.lower()[0], yeh.upper()[0] ]
accept['no'] = [ neh, neh.lower(), neh.upper(), neh.lower()[0], neh.upper()[0] ]
return accept
accepted = makeChoi('Yes', 'No')
def doYeh():
print('Yeh! Let\ do it.')
def doNeh():
print('Neh! Let\ not do it.')
choi = None
while not choi:
choi = input( 'Please choose: Y/n? ' )
if choi in accepted['yes']:
choi = True
doYeh()
elif choi in accepted['no']:
choi = True
doNeh()
else:
print('Your choice was "{}". Please use an accepted input value ..'.format(choi))
print( accepted )
choi = None
Ответ 13
Как насчет этого:
def yes(prompt = 'Please enter Yes/No: '):
while True:
try:
i = raw_input(prompt)
except KeyboardInterrupt:
return False
if i.lower() in ('yes','y'): return True
elif i.lower() in ('no','n'): return False
Ответ 14
Это то, что я использую:
import sys
# cs = case sensitive
# ys = whatever you want to be "yes" - string or tuple of strings
# prompt('promptString') == 1: # only y
# prompt('promptString',cs = 0) == 1: # y or Y
# prompt('promptString','Yes') == 1: # only Yes
# prompt('promptString',('y','yes')) == 1: # only y or yes
# prompt('promptString',('Y','Yes')) == 1: # only Y or Yes
# prompt('promptString',('y','yes'),0) == 1: # Yes, YES, yes, y, Y etc.
def prompt(ps,ys='y',cs=1):
sys.stdout.write(ps)
ii = raw_input()
if cs == 0:
ii = ii.lower()
if type(ys) == tuple:
for accept in ys:
if cs == 0:
accept = accept.lower()
if ii == accept:
return True
else:
if ii == ys:
return True
return False
Ответ 15
def question(question, answers):
acceptable = False
while not acceptable:
print(question + "specify '%s' or '%s'") % answers
answer = raw_input()
if answer.lower() == answers[0].lower() or answers[0].lower():
print('Answer == %s') % answer
acceptable = True
return answer
raining = question("Is it raining today?", ("Y", "N"))
Вот как я это сделаю.
Выход
Is it raining today? Specify 'Y' or 'N'
> Y
answer = 'Y'
Ответ 16
Здесь я беру его, я просто хотел прервать, если пользователь не подтвердил действие.
import distutils
if unsafe_case:
print('Proceed with potentially unsafe thing? [y/n]')
while True:
try:
verify = distutils.util.strtobool(raw_input())
if not verify:
raise SystemExit # Abort on user reject
break
except ValueError as err:
print('Please enter \'yes\' or \'no\'')
# Try again
print('Continuing ...')
do_unsafe_thing()
Ответ 17
Как нуб-программист, я обнаружил, что куча приведенных выше ответов слишком сложна, особенно если цель состоит в том, чтобы иметь простую функцию, на которую вы можете задавать различные вопросы "да/нет", заставляя пользователя выбирать "да" или "нет". Прочитав эту и несколько других страниц и позаимствовав все полезные идеи, я получил следующее:
def yes_no(question_to_be_answered):
while True:
choice = input(question_to_be_answered).lower()
if choice[:1] == 'y':
return True
elif choice[:1] == 'n':
return False
else:
print("Please respond with 'Yes' or 'No'\n")
#See it in Practice below
musical_taste = yes_no('Do you like Pine Coladas?')
if musical_taste == True:
print('and getting caught in the rain')
elif musical_taste == False:
print('You clearly have no taste in music')
Ответ 18
Для Python 3 я использую эту функцию:
def user_prompt(question:str):
""" Prompts a Yes/No questions. """
import sys
from distutils.util import strtobool
while True:
sys.stdout.write(question + " [y/n]: ")
user_input = input().lower()
try:
result = strtobool(user_input)
return result
except ValueError:
sys.stdout.write("Please use y/n or yes/no.\n")
Функция strtobool преобразует строку в тип bool. Если строка не может быть проанализирована, она вызовет ValueError.
В Python 3 raw_input был переименован для ввода.