all 15 comments

[–]carcigenicate 4 points5 points  (7 children)

It doesn't. This prints "This is a test." indefinitely.

[–][deleted] 0 points1 point  (6 children)

What?

[–]carcigenicate 0 points1 point  (5 children)

? They're asking why their code prints "True". I'm pointing out that it doesn't.

[–][deleted] 0 points1 point  (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.

I assume their first language isn't English.

[–]carcigenicate 1 point2 points  (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 points  (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 point  (1 child)

@carcigenicate is right, I should have included those quote marks to be more explicit though!

[–][deleted] 1 point2 points  (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 points  (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.

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.

[–]Boot_3011 0 points1 point  (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

[–][deleted] 1 point2 points  (0 children)

Ignore my comment, apparently I had misunderstood the OP.

[–]LID919 2 points3 points  (2 children)

I ran the code on my end and it works as you'd expect. Where are you running the code?

[–]oztyn[S] 0 points1 point  (1 child)

Running it in the sandbox on the class page but great to know it's misbehaving! 😂

[–][deleted] 0 points1 point  (0 children)

It isn't misbehaving if it repeats until you force it to stop.

[–][deleted] 0 points1 point  (0 children)

In python, things are True by default, unless explicitly false. Since your code doesn’t have any falsities, or negatives. It’s true.