1

I have written a loop that checks if the inputted values are valid, however once an invalid value has been entered once, when the user is prompted to enter values again ALL values become invalid. I can't identify the issue, any help is appreciated.

valid_char = ["k", "q", "r", "n", "b", "p", "K", "Q", "R", "N", "B", "P", "-"]
row = input()
for ch in row:
    while ch not in valid_char: #validating input characters
        print("\nERROR: please enter valid characters")         
        row = input()
5
  • 2
    You get stuck in the while ch not in... You want if ch not in... and to move the authentication loop outside of the for loop. Commented Dec 1, 2017 at 0:28
  • probably because row = input() is inside a while loop, which should just be a conditional branch isntead... Commented Dec 1, 2017 at 0:28
  • When you input another row, you're stuck in the while ch not in valid_char loop, with neither ch nor valid_char changing. Commented Dec 1, 2017 at 0:28
  • could one of you write me an example? I'm having a hard time understanding... Commented Dec 1, 2017 at 0:37
  • when ch is not in valid_char then while ch not in valid_char: stars looping and it can't leave this loop because ch doesn't change - it compare again the same ch which is not in valid_char. You would have to change ch inside while for value from valid_char to leave loop. Maybe use print('ch:', ch) in different places. Commented Dec 1, 2017 at 0:46

1 Answer 1

3

You have two issues in your code. First, you ask for new input, update row, but then only check ch without changing it. If you trace the code line by line you can see what I mean. ch can never change once you get bad input. That's the issue you're describing. You can easily see this if you add the the character that doesn't match to your error message.

    print("ERROR: please enter valid characters --", ch)

The second issue is that your code assumes changing row will restart the loop and read its new values. You can see how that won't work with this example:

some_list = [1,2,3,4,5]
for i in some_list:
    print(i)
    if i == 2:
        some_list = ['a', 'b', 'c']

You expect this example to print:

1
2
a
b
c

But it will print:

1
2
3
4
5

When you assign a new value to row, the loop will keep running over its old value because it already read the value.

Both issues can be found by tracing your code line by line. You can use a debugger, trace it in your head, or add print() statements everywhere to see what happens.

To get this working, you can use something like the following. There are better and cleaner ways to do it, but it should convey the expected logic.

valid_char = ["k", "q", "r", "n", "b", "p", "K", "Q", "R", "N", "B", "P", "-"]
while True:
    row = input()
    all_valid = True
    for ch in row:
        if ch not in valid_char: #validating input characters
            print("\nERROR: please enter valid characters")
            all_valid = False
            break

    if all_valid:
        print('valid!')
        break
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.