while swag: will run while swag is "truthy", which it will be while swag is True, and will not be when you set swag to False.
while swag: will run while swag is "truthy", which it will be while swag is True, and will not be when you set swag to False.
Does while swag - check if swag exists or if swag is True
It checks if swag is True (or "truthy", I should say). And yes, the loop will exit after 3 iterations because i=i+1 must be executed 3 times until i == 3 and (by the if-statement) swag is set to False, at which point the loop will exit.
But why not check this yourself?
swag = True
i = 0
while swag:
i=i+1
print(swag)
if i == 3:
swag = False
True True True
Can You Break A While Loop Via Return Boolean Value From A Function?
need help understanding Boolean statement in while loop
Videos
I feel like it should work, but Python doesn't seem to care about my feelings. So I'm playing around with functions and I feel like I should be able to create a function that returns a boolean value that breaks a while loop in the body of the code. So here's what I've got, sadly, it doesn't seem to actually read what's being returned and just continues to ask for a username. Please, ignore my stupid usernames, I am neither smart no clever. :
def username_checker(username, users,):
"""Checks list of users to find out if username is already taken"""
if username in users:
print("\nSorry that name is already taken.")
return True
else:
print (f"\nWelcome, {username}")
return False
users = ["bbrad", "poopinajimmyhat7", "coolkid28",]
while True:
user_name = input ("\nPlease enter a username: ")
username_checker (user_name, users)Hello I am doing Jose Portillas course on python in udemy and I ran into a bit of code that I am having a hard time wrapping my head around.
def summer_69(arr):
sum69 = 0
cond = True
for num in arr:
while cond:
if num != 6:
sum69 += num
break
else:
cond = False
while not cond:
if num != 9:
break
else:
cond = True
break
summer_69([2, 1, 6, 9, 11])as you can see the variable cond starts with True assigned to it. but in the first while loop if the condition is no longer met (that the variable num is not equal to 6) then the cond will have its value reassigned to False.
so if cond is now false. how come it skips the first while loop considering that the while loops condition is met in the next iteration
and also why, in the second while loop, does it meet the conditions is the variable assigned to cond somehow still True?