2

I am having issues trying to get a while loop to validate the users input, and then make sure the user doesn't repeat values. Shown bellow are the two methods I have tried, but I can't get my head around how to get them to work.

Method 1

def test():
my_list = ["", "", ""]
for i in range(3):
    while (my_list[i] != "one") and \
          (my_list[i] != "two") and \
          (my_list[i] != "three"):

        while (my_list[i] == my_list[0]) and \
              (my_list[i] == my_list[1]) and \
              (my_list[i] == my_list[2]):

            text = "Enter, one, two or three", i + 1, ":"
            try:
                my_list[i] = input(text)
            except KeyboardInterrupt:
                sys.exit()

print(my_list)

Method 2

def test2():

my_list= ["", "", ""]
while len(my_list)!=len(set(my_list)) == True:
    for c in range(4):
        while (my_list[i] != "one") and \
              (my_list[i] != "two") and \
              (my_list[i] != "three"):
            text = "Enter, one, two or three", c + 1, ":"
            try:
                my_list[c] = input(text)
            except KeyboardInterrupt:
                sys.exit()


print(my_list)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What do you mean by 'does not repeat the values' ? Is giving "two" already valid, or do you want a permutation of all values, such as "two", "one", "three" ? – catchmeifyoutry Dec 07 '12 at 17:27

1 Answers1

3

Why not simplify it a bit:

my_list = []
while len(data) < 3:
    data = input(text) # Valid for Python 3, use raw_input(text) in Python 2
    if data in ("one", "two", "three") and data not in my_list:
        my_list.append(data)

This is a more-or-less direct translation of your requirements:

  1. I have a list
  2. While the list is too small
    1. Get some data
    2. If the data is one of the valid values, and not already in the list
      1. Add it to the list

Using for x in range(y) in this case just adds complexity, because you don't actually know how many times you want the loop to run.

There's no reason to pre-fill the list with invalid values (my_list = ["", "", ""]), since lists can be resized with append.

Also, your sys.exit code is unnecessary and probably harmful. Just let the exception propagate and it will crash the program itself (unless you catch it, which you shouldn't almost never do for a KeyboardInterrupt exception).

Note: Your first version would work if you combined the two while loops into one, it's just unnecessarily complicated.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • I think you mean raw_input. Input evaluates the incoming string and is equivalent to eval(raw_input(text)) which will fail with a NameError in the above example. – Alex Couper Dec 07 '12 at 17:30
  • @AlexCouper It [depends on their version of Python](http://stackoverflow.com/a/954840/212555). I'll add a note that they need `raw_input` in Python 2. – Brendan Long Dec 07 '12 at 17:34
  • Thank you for your help @BrendanLong, I am still very new to python and appreciate your time. – Oliver McBlain Dec 07 '12 at 17:55