Fwd: RE: [Tutor] redundant code
matt dittsworth
kthet_02@yahoo.com
Sat May 10 05:00:02 2003
--0-280018374-1052557155=:45755
Content-Type: multipart/alternative; boundary="0-794396427-1052557155=:45755"
--0-794396427-1052557155=:45755
Content-Type: text/plain; charset=us-ascii
Note: forwarded message attached.
Matt DittsworthSemper FiNew Website
---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
--0-794396427-1052557155=:45755
Content-Type: text/html; charset=us-ascii
<DIV><BR><BR>Note: forwarded message attached.</DIV><BR><BR><DIV>
<DIV>Matt Dittsworth</DIV>
<DIV>Semper Fi</DIV>
<DIV><A href="http://www.geocities.com/kthet_02">New Website</A></DIV></DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/search/mailsig/*http://search.yahoo.com">The New Yahoo! Search</a> - Faster. Easier. Bingo.
--0-794396427-1052557155=:45755--
--0-280018374-1052557155=:45755
Content-Type: message/rfc822
X-Apparently-To: kthet_02@yahoo.com via 66.163.169.17; 09 May 2003 21:02:09 -0700 (PDT)
Return-Path: <tutor-admin@python.org>
Received: from 12.155.117.29 (EHLO mail.python.org) (12.155.117.29)
by mta219.mail.scd.yahoo.com with SMTP; 09 May 2003 21:02:09 -0700 (PDT)
Received: from localhost.localdomain ([127.0.0.1] helo=mail.python.org)
by mail.python.org with esmtp (Exim 4.05)
id 19ELYl-0008K6-00; Sat, 10 May 2003 00:02:07 -0400
Received: from rwcrmhc53.attbi.com ([204.127.198.39])
by mail.python.org with esmtp (Exim 4.05)
id 19ELYM-0008Hs-00
for tutor@python.org; Sat, 10 May 2003 00:01:42 -0400
Received: from onegreg (12-246-179-136.client.attbi.com[12.246.179.136])
by attbi.com (rwcrmhc53) with SMTP
id <2003051004011005300j2gj4e>; Sat, 10 May 2003 04:01:10 +0000
From: "Greg Chapman" <greg@gregmchapman.info>
To: "Peter Jakubowicz" <beyondthezero@earthlink.net>
Cc: <tutor@python.org>
Subject: RE: [Tutor] redundant code
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0)
In-Reply-To: <5.2.1.1.1.20030509200739.00b27290@earthlink.net>
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700
X-warning: 204.127.198.39 in blacklist at unconfirmed.dsbl.org
(http://dsbl.org/listing?ip=204.127.198.39)
X-Spam-Status: No, hits=1.3 required=5.0 tests=IN_REP_TO,RCVD_IN_MULTIHOP_DSBL,RCVD_IN_UNCONFIRMED_DSBL,SPAM_PHRASE_01_02
X-Spam-Level: *
Sender: tutor-admin@python.org
Errors-To: tutor-admin@python.org
X-BeenThere: tutor@python.org
X-Mailman-Version: 2.0.13 (101270)
Precedence: bulk
List-Help: <mailto:tutor-request@python.org?subject=help>
List-Post: <mailto:tutor@python.org>
List-Subscribe: <http://mail.python.org/mailman/listinfo/tutor>,
<mailto:tutor-request@python.org?subject=subscribe>
List-Id: Discussion for learning programming with Python <tutor.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/listinfo/tutor>,
<mailto:tutor-request@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/tutor/>
X-Original-Date: Fri, 9 May 2003 21:00:57 -0700
Date: Fri, 9 May 2003 21:00:57 -0700
Content-Length: 716
>there's a better way to do what I've done. Anyway, I've written this little
>thing to prompt for answers to simple multiplication problems, and I'm
>wondering if anyone would care to comment on it. It bothers me that I have
>to use the exact same line twice, but I can't figure out how to change
>that. Plus it bothers me that the whole thing doesn't fit into one
>function. Anyway, I'd appreciate any comments.
I'm something of a newbie myself, so I can't completely answer your question
(I can't think of how to make it one function cleanly) but this gets rid of
some of the redundant code:
from random import randrange
from sys import exit
from string import lower
def question():
n1 = randrange(1, 10)
n2 = randrange(1, 10)
answer = int(raw_input('How much is %d times %d? ' % (n1, n2)))
if answer != n1 * n2:
return 1
else:
return 0
while 1:
if question():
print "No. Try again."
else:
print "Yes, that is correct."
again = str(raw_input("Would you like another question (type Y or
N)?"))
if lower(again) == "y":
pass
else:
exit()
HTH, and I'm sure someone else will chime in with even better ways of doing
it.
greg
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
--0-280018374-1052557155=:45755--