1

In trying to create a while loop, I am running into a perplexing issue where the break is not working as intended. The expected behavior is that once the profile name has been found in Mongo DB, the while loop breaks and the rest of the script is executed. If the profile name is not found, then the loop should start again and ask the user to re-enter their profile name.

Here is the example of the while loop.

while True:
    profile_name = input("\nEnter Profile Name (Case Sensitive): ")
    if len(profile_name) == 0:
        print("Profile cannot be blank!\n")
        pass
    for profile in prof_collection.find({"name": profile_name}):
        if profile_name in profile["name"]:
            print("%s has been found." % profile_name)
            break
        else:
            print("%s was not found. Please enter a valid Profile." % profile_name)
            pass

Any assistance here would be greatly appreciated.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MrY
  • 13
  • 3

1 Answers1

1

You can set a variable as a flag.

looper = True # the flag
while looper:
profile_name = input("\nEnter Profile Name (Case Sensitive): ")
    if len(profile_name) == 0:
        print("Profile cannot be blank!\n")
        pass
    for profile in prof_collection.find({"name": profile_name}):
        if profile_name in profile["name"]:
            print("%s has been found." % profile_name)
            looper = False # flag changes
            break
        else:
            print("%s was not found. Please enter a valid Profile." % profile_name)
            pass
codrelphi
  • 1,075
  • 1
  • 7
  • 13