Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!news.unit0.net!news2.euro.net!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'beginner': 0.04; 'mode,': 0.07; 'subject:while': 0.07; 'append': 0.09; 'am,': 0.14; 'subject:file': 0.14; 'wrote:': 0.14; "'r')": 0.16; '*always*': 0.16; '*never*': 0.16; 'executed.': 0.16; 'subject:user': 0.16; 'input': 0.17; 'cc:addr:python-list': 0.17; 'tue,': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'file,': 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'clause': 0.23; 'url:mailman': 0.26; 'received:209.85.161': 0.26; 'message-id:@mail.gmail.com': 0.28; 'mode': 0.29; '24,': 0.29; "python's": 0.29; 'subject:HELP': 0.29; 'cc:addr:python.org': 0.30; 'url:listinfo': 0.30; "'a')": 0.30; 'skip:\xc2 20': 0.30; '\xc2\xa0\xc2\xa0\xc2\xa0': 0.30; 'print': 0.31; 'break': 0.33; 'chris': 0.34; 'file': 0.34; 'implies': 0.35; 'open': 0.36; 'similar': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'user': 0.39; 'received:209': 0.39; 'attempt': 0.39; 'current': 0.40; 'help': 0.40; 'addition': 0.60; 'subject:, ': 0.60; 'best': 0.60; 'your': 0.60; 'below': 0.61; 'opened': 0.63; 'dear': 0.63; '8bit%:100': 0.68; 'skip:\xc2 10': 0.72; 'press': 0.72; 'directed': 0.80; 'sender:addr:chris': 0.84; 'url:tutor': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=V8RJ0QJ0J6N38qQXsn7LE9CyesL+kJW5t8ZU47dFJ/0=; b=U5UJMOS/S6hu250QEzAzUm1Co+sqRSKtTLjwn2lJiDLGU+/9yyNt3R5pkFrOXwqrul aAMG4Q0aSaEVD3eS9/3FRnkF1JaJSLb5YxnX7uyfVL9ri/TmnnzWbBVMNmaAAIFOEcOV 0h0DvF8PEzWsSoeXTnTuzCSxde+G496p7SZHU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=AAcGGWjLIR0Wb+k8aOYgpZNt5asfcs828QtJCpKYUQJU4gQvnKJ+kBycL0VEBfY+9o tpRju/99+x9sfRbItA44sZ/EESLDBc5QWcNsuoFWW+pL8UmSxbjTf2tQqf43f7rl9Avk 3ImOYymCcbaJRJC/UVAboT27j54AYOp6O+dBg= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Tue, 24 May 2011 01:58:25 -0700 X-Google-Sender-Auth: n_MmJFOFPUPFsl9KV5wrR-p6By4 Subject: Re: NEED HELP- read file contents, while loop to accept user input, and enter to exit From: Chris Rebert To: Cathy James Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 40 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306227882 news.xs4all.nl 49038 [::ffff:82.94.164.166]:39543 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6125 On Tue, May 24, 2011 at 1:31 AM, Cathy James wrote: > dear mentor, > > I need help with my code: In addition to what others have already said... > please see my attempt below and help: > > #1) open file and display current file contents: > f =3D open ('c:/testing.txt'', 'r') > f.readlines() > #2)=C2=A0 and 3) use while loop=C2=A0 to write user input to file, save t= o file, close > when press enter: > while True: > =C2=A0=C2=A0=C2=A0 s =3D input ('enter name: ').strip() > =C2=A0=C2=A0=C2=A0 f =3D open ('c:/testing.txt', 'a') > =C2=A0=C2=A0=C2=A0 if f.writable(): Since you *just* opened the file in append mode, this condition will *always* be true (append mode implies writability), so your `else` clause will *never* be executed. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 f.write(s) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 break > =C2=A0=C2=A0=C2=A0 else: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 f =3D open ('c:/testing.txt', = 'r') > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 f.readlines() > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 for line in f: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print = (line) Similar beginner questions would be best directed to Python's Tutor mailinglist: http://mail.python.org/mailman/listinfo/tutor Cheers, Chris