Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'elif': 0.05; 'test,': 0.07; 'false,': 0.09; 'will,': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'computes': 0.16; 'evaluates': 0.16; 'executed,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helps!': 0.16; 'introduces': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'thanks.': 0.20; '>>>': 0.22; 'aug': 0.22; 'this?': 0.23; 'error': 0.23; 'instance,': 0.24; 'compare': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'possibility': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'branches': 0.31; 'prints': 0.31; 'probably': 0.32; 'bugs': 0.33; 'plain': 0.33; 'received:google.com': 0.35; 'false': 0.36; 'right?': 0.36; 'doing': 0.36; 'next': 0.36; 'two': 0.37; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'hope': 0.61; 'matter': 0.61; 'first': 0.61; 'chance': 0.65; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=dP02JVMownMXsM+kkVfLs0h39GLxVWEJLBg/e2ZihE0=; b=f1FDu116BhZqPb57bVr+5yllMctzDuTFPHjeWvQSlb9+g8g6Q8SY6aGnmYIewsHbNQ 7WuYVLaiSBKn8LeuZGQpEfMx03ALdoaXZDChp5wnQx3MwRPxjaD5rUqgPkNfbP5bqQMG 7S3dgr6Smny8TRaQiyezG9ahRw6PdvSBBkea9bsxe/5j+qv2Mwg8h4ng3DsLo8sTJw9o oS5PZLX4o4P8+pkcC+vEjOBGDMs0P18cZ1KHCCl2BmKITQ42YnIf+x/iyam3ptJ/qrsP DFTpitH+qdsauhT2aSZgL1f06aSx/cLVJkUGkAIK1PZY6YU3Iy+K4+wYYhmKAAskswQx tvng== MIME-Version: 1.0 X-Received: by 10.52.94.78 with SMTP id da14mr90247vdb.28.1375827032362; Tue, 06 Aug 2013 15:10:32 -0700 (PDT) In-Reply-To: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> References: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> Date: Tue, 6 Aug 2013 23:10:32 +0100 Subject: Re: Beginner question 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375827040 news.xs4all.nl 15903 [2001:888:2000:d::a6]:38578 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52069 On Tue, Aug 6, 2013 at 10:35 PM, wrote: > Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks. > > def thing(): > print('go again?') > goagain=input() > if goagain=='y' or 'yes': > print('ok') > elif goagain!='y' or 'yes': > print('goodbye') > sys.exit() > thing() When you use 'or' in this way, it's not doing what you think it does. It actually first computes >>> goagain=='y' which is either True or False, and then evaluates the next part: >>> True or 'yes' >>> False or 'yes' Try out those two in interactive Python and see what they do. You probably want to compare in both halves of the test, or possibly use the 'in' operator: if goagain in ('y', 'yes'): Also, you don't need an 'elif' there; just use a plain else. Repeating the condition just introduces the chance of bugs - for instance, would you notice the error in this? if goagain=='y' or goagain=='yes': print('ok') elif goagain!='y' or goagain!='yes': print('goodbye') Using a plain 'else' guarantees that exactly one of the branches will be executed, rather than having the possibility that neither will, which isn't the intention here. Hope that helps! ChrisA