-2

All I'm trying to get is for the input list_ to only be 0-10 and when it is not, have the user input it again until they do. So if they enter 'spacebar', 'p', '[' or anything along those lines it should only be valid for 0-10. This is all I could come up with:

list1 = ["bacon", "cheese", "cheddar", "milk", "apples", "lamp", "door", "blanket", "rug", "windows", "coat"]
list_ = input("What item do you want? (0-10) ")
while True:
    if (list_ != "0") and (list_ != "1") and (list_ != "2") and (list_ != "3") and (list_ != "4") and (list_ != "5"):
        list_ = input("What do you want from the list? (0-10)")
        if (list_ != "6") and (list_ != "7") and (list_ != "8") and (list_ != "9") and (list_ != "10"):
            list_ = input("What do you want from the list? (0-10)")
    elif list_ == "0":
        print(list1[0])
        break
    elif list_ == "1":
        print(list1[1])
        break
    elif list_ == "2":
        print(list1[2])
        break
    elif list_ == "3":
        print(list1[3])
        break
    elif list_ == "4":
        print(list1[4])
        break
    elif list_ == "5":
        print(list1[5])
        break
    elif list_ == "6":
        print(list1[6])
        break
    elif list_ == "7":
        print(list1[7])
        break
    elif list_ == "8":
        print(list1[8])
        break
    elif list_ == "9":
        print(list1[9])
        break
    elif list_ == "10":
        print(list1[10])
        break
Axhul
  • 47
  • 3
  • `print(list1[int(list_)]`. I suggest using better names. For example `list_` could be `index` and `list1` could be `groceries`. – Code-Apprentice Aug 27 '20 at 16:46
  • 1
    Why not convert the input into an integer using `int()` and the just use it as an index? `int(input( ))` is a common idiom. – John Coleman Aug 27 '20 at 16:46
  • here's a start: `print(list1[int(list_)]` – Paul H Aug 27 '20 at 16:46
  • `if list_ not in ["0", "1", "2", ..., "10"]:` – Barmar Aug 27 '20 at 16:47
  • You should do the input validation in a loop, not just 2 times. – Barmar Aug 27 '20 at 16:47
  • You can just convert your input to an int from the start and then easily check if it's in a range... `if int(list_) in range(10)` – Tomerikoo Aug 27 '20 at 16:47
  • @Barmar or even: `if int(list_) not in list(range(11)):` – Paul H Aug 27 '20 at 16:47
  • Does this answer your question? [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) – Tomerikoo Aug 27 '20 at 16:48
  • @PaulH But that will raise an exception if the input isn't an integer. I couldn't fit the exception handling into my 1-line comment. – Barmar Aug 27 '20 at 16:49

1 Answers1

3

Lists are indexed by integer so if you convert the input from string to integer its a one liner. But you have to worry about bad input data. Commonly in python, you just try the operation and catch any exceptions that occur.

list1 = ["bacon", "cheese", "cheddar", "milk", "apples", "lamp", "door", "blanket", "rug", "windows", "coat"]
while True:
    list_ = input("What item do you want? (0-10) ")
    try:
        print(list1[int(list_)])
        break
    except (IndexError, ValueError):
        print(list_, "is not a number between 0 and 10")
tdelaney
  • 73,364
  • 6
  • 83
  • 116