Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43191
| References | <b93147e7-9d17-4232-99a3-767b88f9e9ba@googlegroups.com> <596ca4b8-b5aa-4112-b086-6320108075f7@googlegroups.com> <mailman.348.1365516328.3114.python-list@python.org> <a530b25f-f9d9-45e9-925c-0ecd8a660cf6@googlegroups.com> |
|---|---|
| Date | 2013-04-10 02:10 +1000 |
| Subject | Re: While loop help |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.359.1365523839.3114.python-list@python.org> (permalink) |
On Wed, Apr 10, 2013 at 1:47 AM, <thomasancilleri@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
While loop help thomasancilleri@gmail.com - 2013-04-09 06:32 -0700
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 06:57 -0700
Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:12 -0400
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 10:18 -0700
Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 03:23 +1000
Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:30 -0400
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 06:58 -0700
Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 00:05 +1000
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 08:47 -0700
Re: While loop help Joel Goldstick <joel.goldstick@gmail.com> - 2013-04-09 12:02 -0400
Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 02:10 +1000
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:24 -0700
Re: While loop help Joel Goldstick <joel.goldstick@gmail.com> - 2013-04-09 12:36 -0400
Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 02:47 +1000
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:57 -0700
Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 03:08 +1000
Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 13:27 -0400
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:57 -0700
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:24 -0700
Re: While loop help Walter Hurry <walterhurry@lavabit.com> - 2013-04-09 19:35 +0000
Re: While loop help Dave Angel <davea@davea.name> - 2013-04-09 16:12 -0400
Re: While loop help Walter Hurry <walterhurry@lavabit.com> - 2013-04-09 20:59 +0000
Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 08:36 +1000
Re: While loop help rusi <rustompmody@gmail.com> - 2013-04-10 08:59 -0700
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 08:47 -0700
Re: While loop help Chris Angelico <rosuav@gmail.com> - 2013-04-10 00:00 +1000
Re: While loop help thomasancilleri@gmail.com - 2013-04-09 09:49 -0700
Re: While loop help Larry Hudson <orgnut@yahoo.com> - 2013-04-09 23:44 -0700
Re: While loop help Larry Hudson <orgnut@yahoo.com> - 2013-04-10 20:00 -0700
Re: While loop help jmfauth <wxjmfauth@gmail.com> - 2013-04-09 12:19 -0700
csiph-web