[Tutor] More Converter

bob gailer bgailer at alum.rpi.edu
Sun Mar 23 03:42:10 CET 2008

```wackedd at mac.com wrote:
> I am still in need of more help. Currently I am just trying to get one conversion down, as then I can duplicate it. However I am not sure how to make it Convert. Currently I am working with:
>
> # Converter
> Original = raw_input("Insert inches, feet ")
>

There is a convention in Python to name variables starting with lower
case and reserve initial caps for class names.
Thus original = raw_input("Insert inches, feet ")
> To = raw_input("Insert inches, feet ")
> Variable = int(raw_input("Insert Amount to Convert "))
> if Original == raw_input(feet) and To == raw_input(inches):
>         print Variable*12
>

This might be premature for you, but it is useful to separate data from
logic. If this were my program I'd create a dictionary of conversion
factors thus:

factors = {("feet", "inches") : 12.0, ("yards", "inches") : 36.0, ...}
then look up the user's desire thus:
factor = factors[(original, to)]
print variable * factor

I hope you (1) can understand this and (2) see that it makes program
maintenance and expansion a lot easier.

Once you get the value of a dictionary then there are more steps you
could take:
e.g. you only need entries for going "up". If the user requested
"inches", "feet" the dictionary lookup would fail, then you'd try again
with:
factor = factors[(to, original)]
print variable / factor

if (original, to) in factors:
factor = factors[(original, to)]
print variable * factor
elif (to, original):
factor = factors[(to, original)]
print variable / factor
else:
print "no conversion available for %s to %s" % (original, to)

[snip]

--
Bob Gailer
919-636-4239 Chapel Hill, NC

```