Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.05; 'output': 0.05; 'continuation': 0.07; 'level,': 0.07; 'sys': 0.07; 'subject:help': 0.08; 'string': 0.09; 'currently,': 0.09; 'executed': 0.09; 'restart': 0.09; 'summary.': 0.09; 'try:': 0.09; 'python': 0.11; 'above)': 0.16; 'fine.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'looping': 0.16; 'notably': 0.16; 'summarizes': 0.16; 'true:': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; 'have:': 0.19; 'skip:f 30': 0.19; 'version.': 0.19; 'meant': 0.20; 'code,': 0.22; 'programming': 0.22; 'import': 0.22; 'coding': 0.22; 'putting': 0.22; 'instance,': 0.24; 'integer': 0.24; 'fairly': 0.24; 'regardless': 0.24; 'looks': 0.24; 'this:': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'evaluation': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; 'lines': 0.31; '2009,': 0.31; 'about.': 0.31; 'block,': 0.31; "d'aprano": 0.31; 'sep': 0.31; 'steven': 0.31; 'probably': 0.32; 'another': 0.32; 'sense': 0.34; "i'd": 0.34; 'could': 0.34; 'received:209.85': 0.35; 'except': 0.35; 'received:209.85.220': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'doing': 0.36; 'thanks': 0.36; 'received:209': 0.37; 'nov': 0.38; 'skip:m 40': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'quote': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'most': 0.60; 'conversion': 0.61; 'strictly': 0.61; 'simply': 0.61; "you're": 0.61; 'first': 0.61; 'back': 0.62; 'finish': 0.65; 'talking': 0.65; 'reply': 0.66; 'mar': 0.68; 'condition.': 0.84; '1:47': 0.91; '2013,': 0.91; '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; bh=3T62oCeiprCN3msNFjQcj+A6kPhBbQu0IlOn5ffffh4=; b=espBE01rlbcvqaVHgNkQAdg69UkZslPMWYpELMdE+6UwJqSqEEuCIw6gpCiRw5yuAM zswnJJoxjL6yk4HtBLWmxL8+Bz+bzUbI12qEChMiNAme2XEMSt/bbrZiNajsZVd+dWsn rCsWKM/+yKg7ZlimT3RWW8vuZLo7/x1tqyijuNV9PqsAIZKaDNov7LNBzRfkZXhpnluS iM2XF6WrNoQgVv2H8+/lm6V15szaT8e5+018wfltHuLOFZJZrCKQHaX+dg97zM0MPvSK fQTFH8Lrnfn2dlm8jTGAM24w9tqTbicABmh6JgkqgybIxQGjX2cXECV7WUhs4k/mEC+P eNtw== MIME-Version: 1.0 X-Received: by 10.52.37.109 with SMTP id x13mr17134256vdj.10.1365523829499; Tue, 09 Apr 2013 09:10:29 -0700 (PDT) In-Reply-To: References: <596ca4b8-b5aa-4112-b086-6320108075f7@googlegroups.com> Date: Wed, 10 Apr 2013 02:10:29 +1000 Subject: Re: While loop help From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365523839 news.xs4all.nl 2592 [2001:888:2000:d::a6]:43588 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43191 On Wed, Apr 10, 2013 at 1:47 AM, wrote: > ... I'm not sure what version I'm using ... Try putting these lines into a Python script: import sys print(sys.version) That, on any version of Python (back a fairly long way, Steven D'Aprano can probably say how far), will give you a line or so of output that summarizes your version. For instance, I can get the following: 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22) [GCC 4.4.1] 3.4.0a0 (default:5dcd7ee0716a, Mar 30 2013, 08:17:06) [GCC 4.7.2] It's a handy system summary. > choice = raw_input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to Square-Miles\n") > if choice == 1: You probably want to use int(raw_input(...)) here; currently, you're going to get back a string eg "1", which is not equal to the integer 1. But at least now you don't have the automatic evaluation happening :) > restart = raw_input("If you would like to perform another conversion type: true\n") > > Not sure what you meant to exactly by this: > "There's a lot of duplicated code here, most notably your continuation > condition. You can simply back-tab after the elif block and have some > code that reunites all the branches; this would also make things > clearer" Notice how you have the "restart = " line (which I quote above) duplicated into each of your code branches? That's what I'm talking about. Here's a pseudocode version of the looping you have: while restart: choice = get_choice() if choice == 1: do_choice_1() restart = get_restart() elif choice == 2: do_choice_2() restart = get_restart() elif choice == 3: do_choice_3() restart = get_restart() Here's how you could deduplicate that: while restart: choice = get_choice() if choice == 1: do_choice_1() elif choice == 2: do_choice_2() elif choice == 3: do_choice_3() restart = get_restart() The restart line is unindented one level, which brings it out of the if/elif block, and then it'll get executed regardless of 'choice'. (To strictly match your original code, you'd need to finish the elif block with "else: continue", but the code makes at least as good sense without it, so I'd consider that optional.) > Thanks for your reply and if you have any ideas for me to improve my coding that will prevent me from learning python in a sloppy way. I'd like to learn it correctly the first time! You're doing fine. The general pattern of programming is: while True: write_code() try: figure_out_what_is_wrong_with_code() except LackOfSkillException: mail("python-list@python.org",smart_question()) run_code() So far, looks like you're following it just fine. :) ChrisA