Search code examples
pythonlistwhile-loopin-operator

or condition in while loop in python with in operator


 def we():

    t="s"
    r=list(range(0,2))
   # here condition is if LHS equal to RHS then while loop will be run 
    while t.isdigit()==False or int(t) in r==False:
         t=input("enter element here")
         #digit check
         if t.isdigit()==False:
            print("enter element is not a digit")
         if t.isdigit():
            if int(t) in r:
              pass
            else:
               print("inuput element range should be between 0 to 2")
               
    return int(t)

`so in this i need to take input from the user. input should be digit from selected range from r. it check the input element is string or digit but it does not check that the digit is in the range of r and the while loop get closed basically it check only 1st condition in while loop it's not cheking 2nd condition.

`


Solution

  • Rewrite the condition as:

    while not t.isdigit() or int(t) not in r:
    

    I think the problem is that you are comparing if int(t) is in r==False