I have a long function in which I am checking for different parameters and if any of the parameters is False, I don't want to execute the code further.
For your understanding, this is how I want to make it work
Email = True
while(Email == True):
print("Execute Me")
Email = False # Break the while loop here
print("Never execute me")
Here is the pseudo version of my code:
def users_preferences(prefs):
for pref in prefs:
send_email = True
while(send_email == True):
# Set send_email = False if email is not verified and don't move to the next line
# Set send_email = False if user's email is not a part of specific group
...
...
How can I break the loop if the condition is False at any point without further executing the code?
Edit: The problem with break
statements is that it will become cumbersome to check the condition before running a new statement where you have number of statements
You can use a normal break
statement:
Email = True
while Email:
print("Execute Me")
Email = False # Break the while loop here
if not Email:
break
print("Never execute me")
Edit: If the while loop doesn't do anything special, the code can be modified to be:
for pref in prefs:
if not is_email_verified(email) or not is_user_in_group(user, group):
continue
send_email(email)
Hello your code needs to be better indented. Also why haven't you tried using break statement to get out of the loop?
Email = True
while Email:
# do something
break
# continue doing something
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.