While loop help

Chris Angelico rosuav at gmail.com
Tue Apr 9 12:47:05 EDT 2013


On Wed, Apr 10, 2013 at 2:24 AM,  <thomasancilleri at gmail.com> wrote:
> For system version I get this:
> 2.7.2 (default, Oct 11 2012, 20:14:37)
> [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]

Lovely! Perfect.

> But, what I don't understand exactly is the while statement. I've been looking around a lot lately and notice that people say to use for instance:
>
> while restart: or while true: or while restart = true:

while x:
  y

will check whether x "feels trueish", and if it does, will execute y,
then go back and check x again. The general principle is that
something is true and nothing is false - for instance, 0 and 0.0 are
false, while 42 and 0.143 are true. Same goes for lists and such; an
empty list is false, a list with something in it is true.

When you make an actual comparison, you'll get back a result that,
normally, will be one of the strict bool objects True and False. For
instance, the expression:

restart == "true"

will be True if restart has the string "true", and False if it has any
other string.

> What makes a statement true? Is there a way to return a true or false message. My method was to ask the user to type "true" and if that print statement matched restart = "true" then the loop would continue but i imagine there is a better way then matching strings and integers like i have been.

You can print anything, even the boolean values! :) Try it!

Your method works fine. Since you're getting something with
raw_input(), you're working with strings; whatever the user enters,
that's what you work with. You could make it more friendly by checking
just the first letter and case insensitively, and making it "Continue?
Y/N", but that's optional.

> Also what confuses me is that python doesn't use brackets. How do I contain all of my if/else statements into one while loop? Do I have to indent each line of code and extra indentation? I'm used to highschool doing c++ and java when I would just say:
>
> while (x<3)
> {
>      if ()
>     else ()
> }

It's conventional in C++ to indent every block of code.

int main()
{
    //Indent one level
    initialize()
    while (...)
    {
        //Indent two levels
        do_stuff()
        if (...)
        {
            //Indent three levels
            do_more_stuff()
        }
        do_less_stuff()
    }
    close_all()
}

Now, just delete all those lines with nothing but braces. You can
still see the program's logical structure:

int main()
    //Indent one level
    initialize()
    while (...)
        //Indent two levels
        do_stuff()
        if (...)
            //Indent three levels
            do_more_stuff()
        do_less_stuff()
    close_all()

This is how Python works. (And it's almost legal Python syntax, too.
Add a few colons, fix the comments, pretty much done.) For better or
for worse, Python depends on the indentation; but 99%+ of the time,
you would have that indentation even if it didn't matter. (Personally,
I prefer explicit braces. The duplicated information at times helps
catch bugs, and sometimes I format code according to a logical
structure that doesn't necessarily match its physical structure. It's
a freedom I don't often make use of, but it's one that Python denies
me... as I said, for better or for worse. There are those who argue
that that's a freedom I shouldn't have.)

ChrisA



More information about the Python-list mailing list