0

I am trying to use the While loop to validate a user response. I want user to enter "Y" or "N" to continue and kepe asking until a valid response is sought using while loop. I aks for user response within block and I am not using break as i am thinking having a valid response shoud break out of loop automatically. Despite receiving valid user response, the while loop keeps going. Below is my code

enter code here

fruit_list = ["apple", "banana", "cherry", "gooseberry", "kumquat", "orange", "pineapple"]
find = "Y"

while find == "Y":
    fruit_found = False
    print(fruit_list)
    fruit = input("Enter the fruit you want to find in the list above: ").lower()
    n=0
    for i in fruit_list:
        if fruit == fruit_list[n] and fruit_found==False:
            
            print("the fruit you are searching is on position", n+1, "in the fruit list")
            fruit_found = True    
        else:
            n+=1
                        
    if fruit_found == False:
        print("the fruit you are searching is not in the list")        
    
    find = input("Do you want to find another fruit? Enter Y for Yes and N for No: ").upper()   

    #work in progress to validate the response to continue. Y or N
    
    **while find != "Y" or "N" :
            print("Entered response is not valid. ")
            find = input("Test Do you want to find another fruit? Enter Y for Yes and N for No: ").upper()**
    
    
print("Thank you")
G. Anderson
  • 5,815
  • 2
  • 14
  • 21
  • 1
    https://stackoverflow.com/q/15112125/3001761 – jonrsharpe Jan 05 '22 at 19:13
  • What is your question exactly? – Никола Хилендаров Jan 05 '22 at 19:15
  • 1
    `find != "Y" or "N"` is not doing what you hope it does. Though `find not in ("Y", "N")` might. – JonSG Jan 05 '22 at 19:15
  • sorry this is my first time posting, i am not quite sure how this forum works, i tried editting but still seem to not show the entire post and code. – Hitesh Bhardwaj Jan 05 '22 at 19:18
  • the problem i am facing is with this while loop. I would have thought that having a valid response would exit the loop, but it keep iterating.: while find != "Y" or "N" : print("Entered response is not valid. ") find = input("Test Do you want to find another fruit? Enter Y for Yes and N for No: ").upper() – Hitesh Bhardwaj Jan 05 '22 at 19:20
  • 2
    @JonSG Yes "find not in ("Y", "N")" surely worked exactly as i wanted. Thanks a lot. What was happening with the way i was trying to do? – Hitesh Bhardwaj Jan 05 '22 at 19:29
  • 1
    A couple of others have posted links that will answer that. The quick and dirty answer is the `or "N"` part is not connected to `find` it is being interpreted as a stand alone test and any non-empty string (such as `"N"`) is "truthy". :-) – JonSG Jan 05 '22 at 19:41
  • 1
    `find != "Y" or "N"` is interpreted as `(find != "Y") or ("N")`, and `"N"` being treated as `True`, the condition for the `while`-loop was always `True`. – Alex867 Jan 05 '22 at 19:45
  • Thanks, everyone. Very helpful. so glad to receive amazing support. So glad to have joined this community. – Hitesh Bhardwaj Jan 05 '22 at 20:37

1 Answers1

0

This is going to work for you:

while find not in ["Y", "N"]: print("Entered response is not valid. ") find = input("Test Do you want to find another fruit? Enter Y for Yes and N for No: ").upper()

Basically you are asking if the "find" variable equals "Y" or if "N" is an empty string - which obviously is not true. You can also do it that way:

while find != "Y" and find != "N":