-5

How can I go about creating a input and output with Y\N (yes or no) function in a question?

My example; if my question is Would you like some food? (Y \ N):, how can I do this and have the answers show Yes, please. or No, thank you. for either choice and then proceed to the next question with the same function?

I thought about using this: valid=("Y": True, "y": True, "N": False, "n": False) but that only shows up as True or False for me, or is there a way to change from True \ False to Yes \ No? or this one:

 def user_prompt(yes_no): 
      while True: 
           user_input=input(yes_no)

But I'm really not sure how else to proceed with this one, or if there's any other easier solution to this.

KPS
  • 27
  • 6
  • 4
    Use contidional statement with your `valid` variable, then print your desired output – Joseph Nov 22 '19 at 04:17
  • 3
    Possible duplicate of [APT command line interface-like yes/no input?](https://stackoverflow.com/questions/3041986/apt-command-line-interface-like-yes-no-input) – Jab Nov 22 '19 at 04:18
  • @jab, I'm more looking for a more simple, easier way to do the input - the accepted answer is what I had originally looked at but the result was not what I wanted. – KPS Nov 22 '19 at 04:43
  • Another possible duplicate: [While Loop With Yes/No Input (Python)](https://stackoverflow.com/questions/47735267/while-loop-with-yes-no-input-python) – wwii Nov 22 '19 at 04:53

4 Answers4

0

I think what you're looking for is a conditional print statement rather than a true/false return statement on the function.

For example:

def user_prompt():
while True:
    user_input = input("Would you like some food? (Y \ N)")
    print ("Yes, please" if user_input == 'Y' else "No, thank you")

Or, more readable:

def user_prompt():
while True:
    user_input = input("Would you like some food? (Y \ N)")
    if (user_input == 'Y'):
        print("Yes, please")
    elif (user_input == 'N'):
        print("No, thank you")
0

I hope I understood your question correctly, you basically check the first letter (incase user enters yes/no) every time the user enters a value and try to verify that if it's Y/N you break the loop if not you keep asking the same question.

def user_prompt(yes_no): 
    while True: 
        user_input=input(yes_no)
            if user_input[0].lower() == 'y':
                print("Yes, please.")
                break
            elif user_input[0].lower() == 'n':
                please("No, thank you.")
                break
            else:
                print("Invalid, try again...")
Marsilinou Zaky
  • 1,038
  • 7
  • 17
0

Not sure if this is the best way, but I have a class based implementation of same

""" Class Questions """

class Questions:
    _input = True

    # Can add Multiple Questions
    _questions = [
        'Question 1', 'Question 2'
    ]

    def ask_question(self):
        counter = 0
        no_of_question = len(self._questions)
        while self._input:
            if counter >= no_of_question:
                return "You have answred all questions"
            user_input = input(self._questions[counter])
            self._input = True if user_input.lower() == 'y' else False
            counter += 1
        return "You have opted to leave"

if __name__ == '__main__':
    ques = Questions()
    print(ques.ask_question())
Amit
  • 244
  • 1
  • 2
  • 11
0

Firstly there is many ways you can go around this, but I am guessing you found the solution yourself already but here is one that is the best.

    def get_yes_no_input(prompt: str) -> bool:
    allowed_responses = {'y', 'yes', 'n', 'no'}
    user_input = input(prompt).lower()
    while user_input not in allowed_responses:
        user_input = input(prompt).lower()
    return user_input[0] == 'y'
    
    continue = get_yes_no_input('Would you like to proceed? [Y/N]: ')

And there we go.