what does the True refer to?
eg if i write:
while True:
print("hi")i know that "hi" will be repeated infinitely, but what is it thats True thats making "hi" get printed?
also, whatever it is thats True, im assuming its always true, so is that why if i typed 'while False', none of the code below would ever run?
sorry if this is a stupid question
edit: also, besides adding an if statement, is there anything else that would break this infinite while loop?
and how would i break the loop, lets say after "hi" has been printed 10 times, using an if statement or whatever else there is that can break it. thanks!
I was reading through this code and I'm just not getting how while loops work when not operator is also used.
https://pastebin.com/5mfBhQSb
I thought the not operator just inversed whatever the value was but I just can't see this code working if that's the case.
For example , the while not sequenceCorrect should turn the original value of False to True, so the loop should run while it's True. But why not just state while True then? And why declare sequenceCorrect = True again? Doesn't it just means while True, make it True? And so on.
The only way it makes sense to me is of the while not loop always means False (just like the while always means as long as it's True) even if the value is supposed be False and should be inverted to True.
So, is that the case? Can anyone explain why it works like that?
Videos
for example, why does this code work:
user = input("Enter marital status: ")
user = user.upper()
valid = False
while not valid:
if user == "M" or user == "S":
valid = True
else:
print ("invalid input")
user = input("Enter marital status: ")
user = user.upper()but this doesn't:
user = input("Enter marital status: ")
user = user.upper()
valid = True
while not valid:
if user == "M" or user == "S":
valid = False
else:
print ("invalid input")
user = input("Enter marital status: ")
user = user.upper()isn't the boolean condition changing in both cases?
can someone just give me some guidelines for using while True/False,e.g. when is it best to use this type of a while loop? that I can follow on my test tomorrow (yes, tomorrow).
Thanks in advance!
I'm doing "automate the boring stuff" and was on an early lesson about while loops. I can't wrap my head around "while not." The code is below
name = ''
while not name:
print('Enter your name:')
name = input()
print('How many guests will you have?')While my limited knowledge, I would write it instead as
name = ''
while name == '':
print('Enter your name:')
name = input()
print('How many guests will you have?')My question is how can they mean the same thing? Both start with name = False/empty. The way I interpret the top code is, while name is not False/empty. The way I interpret the bottom is, while name is equal to False/empty. In my mind, the top would keep looping with any True value. Thanks for your help!
EDIT: formatting
EDIT 2: Finally figured it out. I'm posting this edit for prosperity sake, for any other programming ogres like me. As long as the condition line is True, the loop will run. If the condition is False, the loop will not run. Even though name is False, the condition line is True and the loop will go until something is inputted. Once something is typed in, name becomes True, which would make the condition not name False, ending the loop.
Thanks everyone for helping me understand that! It's so simple now that I see it. I'm super new to Python and made a rookie mistake.
While loops is kinda frustrating I'm 20 days into python and I'm stuck on loops since last 4 days
I'm working on designing an application, and the high level intention is to run the app forever; Sort of like a Daemon service. The only way I can think of doing this is
While True:
Decorator that does the While True: forever.
This really feels like an anti-pattern to me. What are some other ways to keep an app running forever?
I remember seeing a video where the programmer said that using while True loops aren't good.
Can someone give an example of where this might be the case?
What would be better to use? especially, if I am trying to check for user input.
E.g. They have entered a username that is not in the list of usernames.
If you want 'while false' functionality, you need not. Try while not fn: instead.
The condition is the loop is actually a "pre-" condition (as opposed to post-condition "do-while" loop in, say, C). It tests the condition for each iteration including the first one.
On first iteration the condition is false, thus the loop is ended immediately.
I love this pattern:
do {
something();
if (condition) {
break;
}
somethingelse();
.... // yadda yadda
} while (false);I think it is beautiful. It is brilliant. Once you get it (it should not take long), you just have to agree that it makes code a lot easier to read.
Yet people online seem to disagree with this pattern. They claim "it is just a structured goto". But I disagree. It is fundamentally different from a goto. In a goto, you never know where you'll end up after following the label name. This is not the case here. You know exactly where you will land, and hence there is absolutely no spaghettification.
The code is much cleaner this way - everybody hates nested ifs.
Try to change my view, but I think you can't.
https://pastebin.com/NWkKQc1P
It's from Automate the Boring Stuff. I tried running it through the python tutor and it didn't clarify. Is the basic point that there actually isn't anything it's checking to be True, so it's an infinite loop until you trigger the `break`?
PS I figure this is something simple from much earlier that I didn't internalize. I plan to go through all the basic curriculum again to make sure there aren't any gaps in super basic knowledge.
Player_1 = None
While (not Player_1):
Player_1 = input('your name')I'm not new to python, I understand classes and functions etc..but this is really baffling me and I need some help understanding 'None' and the While loop.
I understand that 'not' negates a Boolean value, I get that piece, but None is not a Boolean value, I've searched this and None is its own Object and None is also not equal to False or True.
The while loop runs if the condition evaluates to true, and doesn't run if evaluated to False.
What is making the loop run the first time and not the second time. Obviously, the first time the logic equates to True and the second time to False...But I don't get the logic.
I think it's the "not" which is confusing me, what is the not doing?
Can someone please enlighten me?
Hi, I was coding a program (description in title). I just learnt about using functions, and was wondering why line 4 and line 6 can't be used in such a way. I know that I can just 'return' true or false, but was curious on why this method is unacceptable.
Any help or suggestions are appreciated!!
x = int(input ("What is your number? "))
def is_even (x):
if x % 2 == 0:
is_even(x) == "true"
else:
is_even(x) == "false"
print (f"It is {is_even(x)} that your number is even")aware numerous gaze cable attraction relieved fuzzy resolute obtainable humor
This post was mass deleted and anonymized with Redact
Using break: https://repl.it/repls/SinfulMonstrousProperties
Using False: https://repl.it/repls/MildMulticoloredCases
I am learning about this flag. If you use the False keyword, you can exit the while loop whenever the elif statement = False.
What's the difference between this and just using the break keyword.
EDIT:
break ends your loop right now, whereas setting some flag equal to False means you're going to execute any remaining code in your current iteration of the loop.
Thank you guys so much for your help. r/learnpython is MUCH better than r/javahelp!
What is the difference? I still canโt get it? Both display false
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?
I'm having trouble wrapping my head around the concept of this as I am going through all of the tutorials on programarcadegames.com . This bit comes up at Section 5 and I have seen it previously on the tutorials but ignored it because I couldn't understand what the difference was or why to use one over the other. If anyone could please clear it up for me I would appreciate it very much! Thank you!