Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43189
| 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-09 12:02 -0400 |
| Subject | Re: While loop help |
| From | Joel Goldstick <joel.goldstick@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.357.1365523346.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Tue, Apr 9, 2013 at 11:47 AM, <thomasancilleri@gmail.com> wrote:
> Sorry I'm just starting to learn python and I'm not sure what version I'm
> using to be quite honest. I just started typing it up in Komodo edit (Which
> I'm not personally a fan in particular) I fixed the missing parenthesis
> which fixed the invalid syntax problem. Also, I apologize for submitting
> code that did not run. This is the code I have now before I tried looping
> it again:
>
> #!/usr/bin/env python
>
> #Program starts here
> print "To start the Unit Converter please type the number next to the
> conversion you would like to perform"
>
choice is not converted to an integer. raw_input returns a string.
> choice = raw_input("\n1:Inches to Meter\n2:Millileters to Pint\n3:Acres to
> Square-Miles\n")
>
This will convert choice to an int. Actually, it might not if what you
type is not a number. Then it will cause and exception.
But for now, do this:
> choice = int(choice)
>
or leave choices as a string and make your if statements like if choice ==
'1'
> #If user enters 1:Program converts inches to meters
> if choice == 1:
> #operation = "Inches to Meters"
> number = int(raw_input("\n\nType the amount in Inches you would like
> to convert to Meters.\n"))
> calc = round(number * .0254, 2)
> print "\n",number,"Inches =",calc,"Meters"
> restart = raw_input("If you would like to perform another conversion
> type: true\n")
>
> #If user enters 2:Program converts millimeters to pints
> elif choice == 2:
> #operation = "Milliliters to Pints"
> number = int(raw_input("\n\nType the amount in Milliliters you would
> like to convert to Pints.\n"))
> calc = round(number * 0.0021134,2)
> print "\n",number,"Milliliters =",calc,"Pints"
> restart = raw_input("If you would like to perform another conversion
> type: true\n")
>
> #If user enter 3:Program converts kilometers to miles
> elif choice == 3:
> #operation = "Kilometers to Miles"
> number = int(raw_input("\n\nType the amount in Kilometers you would
> like to convert to Miles.\n"))
> calc = round(number * 0.62137,2)
> print "\n",number,"Kilometers =",calc,"Miles"
> 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"
>
> 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!
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Joel Goldstick
http://joelgoldstick.com
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