0

I created a simple program where a user can add items to their grocery list. After an item is added they're asked if they're done. If they type "yes" the items on the list is printed and the program exits If they type no they can add more items to the list. The problem is that I only want yes or no as the only option. If the user writes anything else I want a message to print out saying you can only write "Yes" or "no". The way I have the code written since the message is at the bottom when the it's printed out the program ends. I'm not sure where to place it. I tried to place inside the while loop but that contradicts with the condition.

mylist = []
are_you_done = "no"
while are_you_done == "no":
    grocery_item = input("Add to Grocery ")
    are_you_done = input("Are you done? ")
    mylist.append(grocery_item)


if are_you_done == "yes":
    for item in mylist:
        print(item)
else:
    print("Please type Yes or No")
  • Putting it in the while loop is on the right track. You need to change your condition so that the loop continues running if the user puts in something other than yes or no, and doesn't exit until they enter yes. – vgel Dec 23 '19 at 22:30

3 Answers3

1

Define:

def ask_yes_no():
    while True:
        x = input()
        if x == "yes":
            return True
        if x == "no":
            return False
        print("Expecting yes/no")

Then use ask_yes_no() in the following manner:

grocery_items = []

while True:
    grocery_item = input("Add to Grocery ")
    print("Are you done? ", end="")

    if ask_yes_no():
        break

    grocery_items.append(grocery_item)
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • And this appends what to what? – roganjosh Dec 23 '19 at 22:31
  • @roganjosh Isn't this what they're asking for in the description? – Mateen Ulhaq Dec 23 '19 at 22:33
  • The _title_ is "Adding items to list using while loop" and they show code appending to a list – roganjosh Dec 23 '19 at 22:35
  • 1
    @roganjosh The majority of the description says, *'After an item is added they're asked if they're done. If they type "yes" the items on the list is printed and the program exits If they type no they can add more items to the list. The problem is that I only want yes or no as the only option. **If the user writes anything else I want a message to print out saying you can only write "Yes" or "no".**'* If you want me to answer the *title*, it's `.append()`, though the OP seems to already be doing that. Besides, the question was closed as a duplicate with almost the same answer as this one. – Mateen Ulhaq Dec 23 '19 at 22:39
  • 1
    The description clearly says that he only wants “yes” or “no”.. – Laurent LAPORTE Dec 23 '19 at 22:39
  • You program doesn't exit. It just exits a function that returns a bool. What are you suggesting they do with that when they have a prompt for more input items? – roganjosh Dec 23 '19 at 22:40
  • @MateenUlhaq, you can even add a *msg* argument to pass to the *input* function. – Laurent LAPORTE Dec 23 '19 at 22:41
  • @LaurentLAPORTE That's true, but I wanted to only prompt the user until after the first input failed. – Mateen Ulhaq Dec 23 '19 at 22:42
  • The issue is that the OP is struggling with in-lined logic, and you just took a _part_ of it, packaged it into a function, and told them nothing about what they were supposed to do with that. Is it a great question? No. But there's no point giving an answer that is clearly more complex than the problem they are already struggling with, in addition to giving no attempt at an explanation – roganjosh Dec 23 '19 at 22:44
0

We need to check within our while loop whether our input conditions have been properly satisified. Also, we should standardize our input for upper and lower case.

mylist = []
are_you_done = "no"
while are_you_done == "no":
    grocery_item = input("Add to Grocery ")
    are_you_done = input("Are you done? ").lower()        
    if are_you_done not in ("no", "yes"):
          print("Please type Yes or No")
          are_you_done = "no"
    else:
          mylist.append(grocery_item)


for item in mylist:
    print(item)
Rahul Chowdhury
  • 641
  • 1
  • 7
  • 16
  • 2
    1) `mylist.append(grocery_item)` needs to come before `mylist.append(grocery_item)`, 2) `if are_you_done != "no" or are_you_done != "yes"` is better as `if are_you_done not in ('no', 'yes')` – roganjosh Dec 23 '19 at 22:34
0

I believe this does what you specified in your question. Notice that the program remains in the inner loop until the answer is yes or no. Notice also that we add the grocery item to the list before asking the question.

#!/usr/bin/python3

mylist = []
are_you_done = "no"

while are_you_done == "no":

    grocery_item = input("Add to Grocery ")
    mylist.append(grocery_item)

    while True:
        are_you_done = input("Are you done? ")
        if are_you_done in ['yes','no']:
            break
        else:
            print( 'please answer with yes or no' )


for item in mylist:
    print(item)
DrM
  • 2,404
  • 15
  • 29