-2
x = input("print data? (Y/N) ")

while (x != 'Y' or x != 'N'):
    x = input("error: wrong input. Please put Y or N only ")

if x == 'Y':
    read_serial()

Trying to check whether or not the keyboard input (x) is equal to a 'Y' or 'N' string. And if not then the loop continues until it is. However the above code seems to compile and run fine except that no matter what the loop keeps running. Don't have much experience with Python 3 yet so could any one tell me what I'm doing wrong?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user3458571
  • 73
  • 1
  • 2
  • 7

2 Answers2

3

This statement

x != 'Y' or x != 'N'

is always True, because everything in the world is not "Y" or not "N".

Change it to:

x != 'Y' and x != 'N'
Maroun
  • 94,125
  • 30
  • 188
  • 241
2

Change the or to and, as you want to check it is not equal to either of these fields. If you want to use or you will have to change the code to

def main ():
    user_input = input("print data? (Y/N) ")

    while (true):
        if (user_input.lower() == 'y' or user_input.lower() == 'n'):
            break
        user_input = input("error: wrong input. Please put Y or N only ")

    if user_input.lower() == 'y':
        read_serial()

side note: x is a poor variable name, call it something more appropriate.

You should always change case to lower when comparing strings when case doesn't matter. In this case, case does not matter so use lower()

def main ():
    user_input = input("print data? (Y/N) ")

    while (user_input.lower() != 'y' and user_input.lower() != 'n'):
        user_input = input("error: wrong input. Please put Y or N only ")

    if user_input.lower() == 'y':
        read_serial()
ChickenFeet
  • 2,653
  • 22
  • 26