Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43311
| Date | 2013-04-10 20:00 -0700 |
|---|---|
| From | Larry Hudson <orgnut@yahoo.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: While loop help |
| References | <b93147e7-9d17-4232-99a3-767b88f9e9ba@googlegroups.com> <05d504a8-ec5b-4cff-b580-213071cf4e28@googlegroups.com> <G5-dnTPyiu_9l_jMnZ2dnUVZ_qudnZ2d@giganews.com> |
| Message-ID | <nqOdnTMDMe7QuvvMnZ2dnUVZ_i2dnZ2d@giganews.com> (permalink) |
On 04/09/2013 11:44 PM, Larry Hudson wrote:
> On 04/09/2013 09:49 AM, thomasancilleri@gmail.com wrote:
>> So what would be the proper way to perform a loop of this program. I still can't quite figure
>> out the best way to do it.
>>
>
> My suggestion... (pseudocode)
>
> # Print a heading/introduction here
> while True:
> # Print menu, with an added selection to quit
> # Get the user's choice (as an int)
> if choice == 1:
> # Print prompt for this choice
> # Enter the value (as float, not int. Why limit your values to ints anyway?)
> # Display the calculated result
> elif choice == 2:
> # Same procedure as above
> elif ... etc
> # etc
> elif choice == (value for quit):
> break # This breaks out of the while loop
> else:
> # Invalid choice, print error message
> # End of loop
>
> Further suggestion:
> Since each of the choices use the same basic procedure, it could be written as a separate single
> function. It would just need to be passed the appropriate prompt string(s) and conversion
> factor. The results display _could_ be in this function also, but that would require passing
> even more strings. It would probably be better to simply return the two values (the input value
> and the converted value) back to the calling block and print the results there.
>
> Also, don't use the round function here, that does NOT guarantee it will be _printed_ to two
> decimal places. Use string formatting in the print statements. For example: (using your
> original variable names, and assuming they are now both floats)
>
> old style:
>
> print '%.2f inches = %.2f meters' % (number, calc)
>
> or new style:
>
> print '{:.2f} inches = {:.2f} meters'.format(number, calc)
>
> You also mentioned that you don't like the editor you're using. For a simple substitute you
> might try Idle (which normally comes with Python). This gives you the advantage of an
> interactive environment as will as an editor. There are many other choices, of course, but as a
> newbie you might find this more comfortable than what you're currently using.
>
> I hope this jump-starts your thinking. Keep at it, it's worth the effort.
>
> -=- Larry -=-
>
On a little further thought, I realized the "single function" I suggested is even easier than I
originally thought -- even with the results printed in the function. Here's an example:
def convert(frm, to, factor):
# frm and to are strings, factor is a float
print 'Converting {} to {}:'.format(frm, to)
value = float(raw_input('How many {}? '.format(frm)))
print '{:.2f} {} is {:.2f} {}'.format(value, frm, value * factor, to)
You would use it like:
convert('inches', 'meters', 0.0254)
or
convert('meters', 'inches', 39.37)
-=- Larry -=-
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