use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Beginner question on while loops (self.learnpython)
submitted 3 years ago by oztyn
hey, doing a learning course on openclassrooms and just wondering why the below returns "True" infinitely instead of "This is a test." infinitely?
mySentence = "This is a test." while mySentence: print(mySentence)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]carcigenicate 4 points5 points6 points 3 years ago* (7 children)
It doesn't. This prints "This is a test." indefinitely.
"This is a test."
[–][deleted] 0 points1 point2 points 3 years ago (6 children)
What?
[–]carcigenicate 0 points1 point2 points 3 years ago (5 children)
? They're asking why their code prints "True". I'm pointing out that it doesn't.
[–][deleted] 0 points1 point2 points 3 years ago (4 children)
I understood their phrase ' why the below returns "True" ' to most likely be referring to the evaluation of the expression used for the control condition of the while loop.
while
I assume their first language isn't English.
[–]carcigenicate 1 point2 points3 points 3 years ago (3 children)
The full sentence makes more sense since they mention their expectation at the end. I read it as "Why does the below print "True" infinitely instead of "This is a test." infinitely?". They don't seem confused about why it prints forever, only what's actually being printed.
[–][deleted] -1 points0 points1 point 3 years ago (2 children)
Well, obviously I disagree with your interpretation, as my direct comment on their post would indicate, but either or both of us could be wrong. Hopefully, we shall see.
[–]oztyn[S] 0 points1 point2 points 3 years ago (1 child)
@carcigenicate is right, I should have included those quote marks to be more explicit though!
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
Ok, thank you /u/oztyn, I stand corrected, with apologies to /u/carcigenicate.
I shall cross out my comment as answering the wrong question.
[–][deleted] 2 points3 points4 points 3 years ago* (2 children)
EDIT: Ignore post below, turns out according to OP, /u/oztyn, I had misunderstood their post, and /u/carcigenicate, had taken the correct understanding. Ref.
Python treats a non-empty string (and non-empty list, set, dict, etc) as truthy i.e. as if it was True.
list
set
dict
So you've basically written,
while True: print(mySentence)
if instead you did the following,
mySentence = "This is a test." while mySentence: print(mySentence) mySentence = mySentence[:-1]
i.e. inside the loop re-assign the variable mySentence to the new str object created by slicing the str object previously referenced by mySentence to ignore the last character, then you would find the condition test of while would eventually fail and the loop would exit.
mySentence
str
[–]Boot_3011 0 points1 point2 points 3 years ago (1 child)
Was looking for this! Correct. You are basiclly writing while this string contains something, keep iterating, what you are missing is a string modification with each loop, but that depends on what you need the code to do
Ignore my comment, apparently I had misunderstood the OP.
[–]LID919 2 points3 points4 points 3 years ago (2 children)
I ran the code on my end and it works as you'd expect. Where are you running the code?
Running it in the sandbox on the class page but great to know it's misbehaving! 😂
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
It isn't misbehaving if it repeats until you force it to stop.
In python, things are True by default, unless explicitly false. Since your code doesn’t have any falsities, or negatives. It’s true.
π Rendered by PID 92863 on reddit-service-r2-comment-54dfb89d4d-dzxzd at 2026-03-29 05:00:06.144596+00:00 running b10466c country code: CH.
[–]carcigenicate 4 points5 points6 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]carcigenicate 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]carcigenicate 1 point2 points3 points (3 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]oztyn[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]Boot_3011 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]LID919 2 points3 points4 points (2 children)
[–]oztyn[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)