Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.05; 'explicit': 0.07; 'lately': 0.07; 'strict': 0.07; 'subject:help': 0.08; 'string': 0.09; 'expression:': 0.09; 'false,': 0.09; 'false.': 0.09; 'integers': 0.09; 'main()': 0.09; 'matched': 0.09; 'restart': 0.09; 'statements': 0.09; 'works.': 0.09; 'python': 0.11; '2.7.2': 0.16; 'bool': 0.16; 'boolean': 0.16; 'brackets.': 0.16; 'colons,': 0.16; 'does,': 0.16; 'fine.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'indent': 0.16; 'instance:': 0.16; 'normally,': 0.16; 'optional.': 0.16; 'perfect.': 0.16; 'statement.': 0.16; 'syntax,': 0.16; 'true:': 0.16; 'worse.': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'wed,': 0.18; 'print': 0.22; 'instance,': 0.24; 'logical': 0.24; "shouldn't": 0.24; 'java': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'matching': 0.30; 'said,': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; 'too.': 0.31; '4.0': 0.31; 'argue': 0.31; 'comments,': 0.31; 'indentation': 0.31; 'lists': 0.32; 'compatible': 0.32; 'checking': 0.33; 'actual': 0.34; 'could': 0.34; 'message.': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'version': 0.36; 'c++': 0.36; 'false': 0.36; 'doing': 0.36; "didn't": 0.36; 'method': 0.36; 'two': 0.37; 'list': 0.37; 'level': 0.37; 'sometimes': 0.38; 'apple': 0.38; 'depends': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'structure': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'according': 0.40; 'how': 0.40; 'even': 0.60; 'catch': 0.60; 'helps': 0.61; "you're": 0.61; 'first': 0.61; 'back': 0.62; 'times': 0.62; "you'll": 0.62; 'making': 0.63; 'information': 0.63; 'more': 0.64; 'levels': 0.65; 'it!': 0.67; 'legal': 0.71; 'friendly': 0.72; 'physical': 0.72; '4.2.1': 0.84; 'imagine': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:content-transfer-encoding; bh=pokEkhLRhf/zwGg1CYCVdMQ8Xvs0tCFeg8L63//2al0=; b=y5nZ4Dyoq4tXqnX8vSt6kyfn9tj1nPuiXQa2/2j6t7yiUE7DWnEx2jZ6ZIUehkhow6 8bFcBUVfF1g/nB+TMsDlfc+9PMmznHH1WRkdQYo517T+GB0p7Xk52g92w5T08dm1EC8U DoSNiDZnta9Fe8ZD0GKHuIEMx7X04ATu/lJl/YNsrxmZA0KqdcUVWfmHK4naSgKh/e0r 4hSKuLWJWFkMabcVdbuW33fHKq1DK6kiA/C4R/mMYhVPJW8af74mB7uRq36evaKZaG6W K/rTZUJsLP+gnDgXrPXE+9/thbhVaH0jUCTRndWgRnERHQrj06Q4Mg/EXgofKDZ0aj0Y Ve+Q== MIME-Version: 1.0 X-Received: by 10.52.91.71 with SMTP id cc7mr2138779vdb.58.1365526025080; Tue, 09 Apr 2013 09:47:05 -0700 (PDT) In-Reply-To: <1629a072-c512-439c-b751-c44f8e945c2e@googlegroups.com> References: <596ca4b8-b5aa-4112-b086-6320108075f7@googlegroups.com> <1629a072-c512-439c-b751-c44f8e945c2e@googlegroups.com> Date: Wed, 10 Apr 2013 02:47:05 +1000 Subject: Re: While loop help From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365526028 news.xs4all.nl 2634 [2001:888:2000:d::a6]:45466 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43199 On Wed, Apr 10, 2013 at 2:24 AM, 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 lo= oking around a lot lately and notice that people say to use for instance: > > while restart: or while true: or while restart =3D 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 =3D=3D "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 mes= sage. My method was to ask the user to type "true" and if that print statem= ent matched restart =3D "true" then the loop would continue but i imagine t= here 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 conta= in all of my if/else statements into one while loop? Do I have to indent ea= ch 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