Apt Command Line Interface-Like Yes/No Input

APT command line interface-like yes/no input?

As you mentioned, the easiest way is to use raw_input() (or simply input() for Python 3). There is no built-in way to do this. From Recipe 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 = 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")

(For Python 2, use raw_input instead of input.)
Usage example:

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

Python yes/no def

The function returns True/False. So use if choice:

Btw, you could have easily found out the solution on your own by adding print choice ;)

Using input to have options 3.4.3

I suspect that a bit of searching would've done you some good on this one.

See answer to extremely similar question here https://stackoverflow.com/a/3041990/5298696

Adding a "Maybe" option would be a trivial edit, provide an update if you need assistance with that.

How to get every combination of a string?

You can use binary combinations 01110, 00011 etc. with itertools.product() to get every combination of cases with a string. This means setting the 1's as uppercase and the 0's as lowercase. So 01110 -> hOUSe, 00011 -> houSE etc.

from itertools import product

def get_all_cases(string):
return [
"".join(
letter.upper() if binary == 1 else letter
for letter, binary in zip(string.lower(), binary_comb)
)
for binary_comb in product([0, 1], repeat=len(string))
]

Output:

>>> get_all_cases("House")
>>> ['house', 'housE', 'houSe', 'houSE', 'hoUse', 'hoUsE', 'hoUSe', 'hoUSE', 'hOuse', 'hOusE', 'hOuSe', 'hOuSE', 'hOUse', 'hOUsE', 'hOUSe', 'hOUSE', 'House', 'HousE', 'HouSe', 'HouSE', 'HoUse', 'HoUsE', 'HoUSe', 'HoUSE', 'HOuse', 'HOusE', 'HOuSe', 'HOuSE', 'HOUse', 'HOUsE', 'HOUSe', 'HOUSE']

You can also just map to True and False boolean values instead of 1 and 0.

from itertools import product

def get_all_cases(string):
return [
"".join(
letter.upper() if is_upper else letter
for letter, is_upper in zip(string.lower(), comb)
)
for comb in product([False, True], repeat=len(string))
]

yesno_prompt( issue

This code shows a fundamental misunderstanding of how input() works in Python 3.

First, the input() function in Python 3 is equivalent to raw_input() in Python 2. Your error shows that you are using Python 2.

So let's read the error message you got:

We know what line the error is on:

  File "mailerproj.py", line 139, in <module>

And it shows us the line:

    ["1"], "Flag this message/s as high priority? [yes|no]")

And then it explains the issue:

TypeError: [raw_]input expected at most 1 arguments, got 2

This means you've given input() the wrong arguments - you've given it two ["1"] and "Flag this message/s as high priority? [yes|no]" and it expects one.

Here is the python help on raw_input:

Help on built-in function raw_input in module builtin:

raw_input(...)
raw_input([prompt]) -> string

Read a string from standard input. The trailing newline is stripped. If the
user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix,
GNU readline is used if enabled. The prompt string, if given, is printed
without a trailing newline before reading.

So you need to give it one argument as input. This can be a string (e.g. "Hello") an integer (e.g. 125), a float (e.g. 3.14), a list, a tuple, a boolean and others. It returns a string into the variable.

You need your code to look more like this:

highpri = raw_input("[1] Flag this message as high priority? [yes|no] > ")
highpri = highpri.upper() # Make the input uppercase for the if
if highpri != 'YES': # If it is not yes, remove the flags.
prioflag1 = ''
prioflag2 = ''
else: # If the input is yes, set the flags.
prioflag1 = '1 (Highest)'
prioflag2 = 'High'

yes/no input loop, issues with with correct looping

while True: will work, you need to break out of the loop once the conditions have been met.

while True:
reply = str(raw_input('\n' + str(abs(y - 5)) + ' - ' + testQ[y] + ' (y/n) --> ')).lower().strip()
if reply in yes or reply in no:
break

Based on the updated scope, try this, it seems the break may have caused you issues:

reply = False
while reply not in yes and reply not in no:
reply = str(raw_input('\n' + str(abs(y - 5)) + ' - ' + testQ[y] + ' (y/n) --> ')).lower().strip()


Related Topics



Leave a reply



Submit