-2

If you ask someone a yes/no question then the answer is one of these two options. In programming what if the response was "Y" or "y" or "yes" or whatever?

I had to create a compound condition repeating my statement while its actually the same one. I'm not an expert but I can see that it can be improved.

def note_maker(note):
    case_note = open("case_note.txt", "a+")
    update = case_note.write("\n" + note)
    multiple_inputs = input("Do you want to enter more details? Y/N")

    while multiple_inputs == "yes" or multiple_inputs == "YES" or multiple_inputs == "Yes" or multiple_inputs == "Y" or multiple_inputs == "y":

        update_again = case_note.write("\n" + input("Enter your additional information"))
        multiple_inputs = input("Do you want to enter more details?")

    case_note.close()

Is there a way to control the user input into what I expect?

smci
  • 32,567
  • 20
  • 113
  • 146
matar770
  • 11
  • 1
  • 1
    Convert the user input to either lower or upper case and check based on the converted string. Such as: `multiple_inputs.lower() == "yes" or multiple_inputs.lower() == "y"` – Spencer Wieczorek Jul 01 '19 at 16:53
  • 1
    @SpencerWieczorek: better is `multiple_inputs.lower() in ('y', 'yes', 'affirmative'...)` – smci Jul 01 '19 at 18:03
  • @SpencerWieczorek, yes and you can use parenthesis for that too to save the creation of a list. – Guimoute Jul 01 '19 at 18:06
  • i think you've prompted the user to enter a `"Y/N"`. The best thing is to then have an `"input invalid, please enter one of Y for yes or N for no"` and just make the user feed the right value in. Usually, by 2 attempts, the user has gotten what they need to know. Imo that's better than trying to figure out what the user entered and giving them free reign to set their cat loose on the keyboard so to speak. – Paritosh Singh Jul 01 '19 at 18:21
  • @matar770 i would suggest to take a look at using membership operator `in` and think of your multiple inputs as a sequence, that's one way to approach a problem – Devesh Kumar Singh Jul 01 '19 at 18:37

3 Answers3

0

You can shorten the user input and make it lowercase which should help.

Eg: user_input = input("Something? y/n: ")[0].lower()

This way, if they enter "Y", "Yes", "yes", or "yep" it will end up being "y".

  • 3
    Alternatively, if you want to accept terms that don't start with 'y', you can shorten the really long `or` conditional to `while multiple_inputs.lower() in ["yes", "y", ...]:` – Green Cloak Guy Jul 01 '19 at 16:58
  • 1
    @GreenCloakGuy rather than using a `list` would a `set` not be better. `O(1)` search time might not make a difference for this question but its a good practice right. – Sash Sinha Jul 01 '19 at 17:10
  • Not this will be _any_ word that starts with "y", which may have some unintended functionality. – Spencer Wieczorek Jul 01 '19 at 17:24
  • @SpencerWieczorek it's a yes or no question, I don't think there are many words beginning with "y" that mean no – Marcus Weinberger Jul 01 '19 at 17:27
  • 2
    @MarcusWeinberger There are plenty of words that start with "y" that doesn't mean yes. It's probably fine here, but it wouldn't be in a production system. – Spencer Wieczorek Jul 01 '19 at 17:33
0

Try refactoring input check into a new function:

def is_affirmative(input: str) -> bool:
    return input.strip().lower() in ['y', 'yes']


def note_maker(note):
    case_note = open("case_note.txt", "a+")
    update = case_note.write("\n" + note)
    multiple_inputs = input("Do you want to enter more details? Y/N")

    while not is_affirmative(multiple_inputs):
        update_again = case_note.write("\n" + input("Enter your additional information"))
        multiple_inputs = input("Do you want to enter more details?")

    case_note.close()

abdusco
  • 9,700
  • 2
  • 27
  • 44
0

You could have a set of valid yes responses and no responses:

yes_responses, no_responses = {'y', 'yes'}, {'n', 'no'}

user_response = None
while user_response not in yes_responses and user_response not in no_responses:
  user_response = input("Do you want to enter more details? (Y/N): ").lower()

if user_response in yes_responses:
  print("The user responded yes.")
else:
  print("The user responded no.")

Example Usage:

Do you want to enter more details? (Y/N): zxc
Do you want to enter more details? (Y/N): YesNo
Do you want to enter more details? (Y/N): yEs
The user responded yes.

Note: The advantage of using a set over a list or a tuple is that is the in operation is O(1) rather than O(N).

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40