While loop help

rusi rustompmody at gmail.com
Wed Apr 10 11:59:04 EDT 2013


On Apr 9, 8:47 pm, thomasancill... at gmail.com wrote:

> ... 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!

Not perhaps a direct answer...
Anyways there is style in which python is best used which people
coming from more traditional languages are usually not familiar with:
its called 'playing around in the interpreter'

Here is a small session based on your code that shows this interaction
with the interpreter:
---------------------
$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def inch2meter(i): return round(i*.254,2)
...
>>> inch2meter(1)
0.25
>>> def milliliter2pint(m): return round(number * 0.0021134,2)
...
>>> milliliter2pint(100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in milliliter2pint
NameError: global name 'number' is not defined
>>> def milliliter2pint(m): return round(m * 0.0021134,2)
...
>>> milliliter2pint(100)
0.21
>>>

---------------------

Notice some things here:
1. I check out the code as soon as its written. So when I cutpasted
from your code, without keeping names (m <-> number) consistent, I get
an error, correct it and continue
2. There is not a single print statement. Not just the functions have
no prints, even the code that calls them has none. Just call get
answer.
This point needs to be underscored: In C or java you cannot write any
useful code without doing IO ie printf/scanf etc. In python you can
and you should try to.



More information about the Python-list mailing list