0

I have a school assignment for Python in which I have a backpack of items and need to make code to ask the user if they want to: a) add an item to the backpack, b) check the items in the backpack, and c) quit the program.

For my code, I want to make it so that if the user at input for adding a new item just hits return and leaves the input blank, it will re-prompt to the input again rather than continuing the code if no item is actually added. Here's what I have so far:

import sys

itemsInBackpack = ["book", "computer", "keys", "travel mug"]

while True:
    print("Would you like to:")
    print("1. Add an item to the backpack?")
    print("2. Check if an item is in the backpack?")
    print("3. Quit")
    userChoice = input()

    if (userChoice == "1"):
        print("What item do you want to add to the backpack?")
        itemAddNew = input()
        if itemAddNew == "":
            break
        else:
            itemsInBackpack.insert(0, itemAddNew)
            print("Added to backpack.")

With my code here, even if I hit return in a test and leave the input blank, the code still continues onward and doesn't break to re-prompt input again. Is it because I'm using an if statement inside of an if statement already? I'm sure in general there's a better way to do this, but as a beginner I'm stumped and could use a shove in the right direction.

ajdbnabad13
  • 355
  • 3
  • 11
  • `break` will escape your while loop thus end the code. Sounds like you want to add another while loop? – busybear Feb 16 '20 at 03:09
  • Interesting, I didn't consider that. I tested it by making a quick while True, if/else, and it's letting me know that my itemAddNew isn't defined. How do I get it to recognize itemAddNew outside of the second while loop? – ajdbnabad13 Feb 16 '20 at 03:14
  • 1
    @cwbusacker's answer might be helpful – busybear Feb 16 '20 at 03:20

1 Answers1

1

The break stops everything in your loop and causes your program to end.

If you want to prompt for input until the user gives you something, change this:

        print("What item do you want to add to the backpack?")
        itemAddNew = input()
        if itemAddNew == "":
            break

to this:

        print("What item do you want to add to the backpack?")
        itemAddNew = input()
        while itemAddNew == "":
            #Added some output text to tell the user what went wrong.
            itemAddNew = input("You didn't enter anything. Try again.\n")

This will keep going WHILE the text is empty.

cwbusacker
  • 507
  • 2
  • 12
  • That makes a lot of sense now, re: break. I misunderstood break and thought it breaks code and returns to the last line of code. Thank you. Your suggestion works great, but PyCharm denotes that it cannot define itemAddNew in the while loop you suggested. Is there a way for it to recognize itemAddNew outside of the loop? I know I can define it above like itemsInBackpack for example, but not quite sure how in this instance. – ajdbnabad13 Feb 16 '20 at 03:21
  • @ajdbnabad13 I'm not sure what you mean by that. Which line does PyCharm not recognize it? There must be something else in your code that is using itemsAddNew where it is not recognized because this works fine. Are you referring to inside the else statement? – cwbusacker Feb 16 '20 at 03:25
  • In my if statement, I am defining itemAddNew = input(). Right below it, I am beginning your suggested while itemAddNew == "":. In that very itemAddNew for the while loop you suggested, PyCharm underlined it yellow and notes that itemAddNew cannot be defined, even though right outside of your suggested loop it is defined. How can I remedy this underline? The code works and runs fine despite this underline. – ajdbnabad13 Feb 16 '20 at 03:27
  • Is it spelled and capitalized exactly the same? ```itemAddNew``` is not the same as ```itemAddnew``` – cwbusacker Feb 16 '20 at 03:29
  • Yes, exactly the same. – ajdbnabad13 Feb 16 '20 at 03:30
  • 1
    It's hard to say because we define it right above the loop AND the variable is identical. If the code runs with the underline, it may just be a problem with PyCharm. – cwbusacker Feb 16 '20 at 03:34
  • Agreed, I'll ignore it. Thanks for your suggestion, it works great and now I understand break better as well. – ajdbnabad13 Feb 16 '20 at 03:35