0

I am working with Python version 3.7.4. I am working on a piece of code that requires user input in the form of yes or no, as follows:

isValid = input("Is this a previous version? (y/n)")

I would like to convert this yes or no question into a boolean response. I have seen this one possibility that I am interested in implementing:

isValid = False if input("Is this a previous version? (y/n)").lower() == 'n' else True

I also want to account for if the user were to give an input other than y or n, though. Ideally, I would like to raise an error if they were to give an input. Could someone show me to how to implement a boolean variable isValid like this in a concise way, or would I have to create another method that would check the user input? Thank you in advance. I can add more details if needed.

user12149354
  • 55
  • 1
  • 7
  • 1
    good thing we dont *have* to write everything in one line, or things would be a mess. Say, why don't you just write an `if elif else statement`? – Paritosh Singh Oct 01 '19 at 15:25
  • If you want to combine this with repeatedly asking the user until they provide a valid response, the idea of a one-liner very quickly becomes untenable. Write a function that hides the code. `isValid = ask_user_whether_valid()` – deceze Oct 01 '19 at 15:28
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – G. Anderson Oct 01 '19 at 15:37

4 Answers4

1

This is a bit more verbose, but will force the user to enter y or n and keep asking until they do:

isValid = input("Is this a previous version? (y/n)")
try:
    while isValid is not True or False:
        if isValid.lower() == 'y':
            isValid == True
            break
        elif isValid.lower() == 'n':
            isValid == False
            break
        else:
            print("please select y or n")
            isValid = input("Is this a previous version? (y/n)")
except:
    print("Please select a valid response")
d_kennetz
  • 5,219
  • 5
  • 21
  • 44
0

Maybe something along these lines?

isValid = input("Is this a previous version? (y/n)").lower() in ['y', 'n']
# if isValid: ... do whatever else: raise error
Kikanye
  • 1,198
  • 1
  • 14
  • 33
0

You could do something like this but it is quite ugly and I don't recommend it at all:

def raiser(ex): raise ex
my_input = input("Is this a previous version? (y/n)").lower()
isValid = False if my_input == 'n' else True if my_input == 'y' else raiser(Exception)

One of the best way to do something like this is a simple if/elif/else:

my_input = input("Is this a previous version? (y/n)")
if my_input == "y":
    isValid = True
elif my_input == "n":
    isValid = False
else:
    raise Exception
Silveris
  • 1,048
  • 13
  • 31
0

A single line way to get a boolean as well as to raise an Exception.

ans = input('y/n : ').lower(); ans1 = False if ans=='n' else (True if ans=='y' else exec('raise(ValueError)'))

ans1 is the boolean answer.