[Tutor] Converter

Alan Gauld alan.gauld at btinternet.com
Thu Mar 20 10:25:00 CET 2008

<wackedd at mac.com> wrote 

> Currently this is what I am working with.

Thanks for posting that it gives us a much better idea of 
what stage you are at and lets us target our answers. 
Forget the Cocoa suggestion I made earlier, your 
basic Python skills need improving before you get 
to that.

> 2 problems I am facing.
> 1. When I run the script and type in the amount of feet it 
> calculates it OVER AND OVER.

Bob already gave you a good suggestion there. Your 
loop repeats while n is greater than zero.  Work through 
the code on paper, as if you were the computer, and 
see when/if n stops being greater than zero.

> 2. Currently this script only works for the amount of 
> feet, is their a way to have it choose feet, inches, 
> to convert into whatever, without having to write 
> the script over and over, and all in different programs. 

Yes of course. All you need is to tell the program which 
units to read and which to convert to. So you need a 
program that looks to the user like:

Input Units (in,cm,ml,km,lbs,kg)> in
Output Units(in,cm,ml,km,lbs,kg) > cm
How many inches? 6
6 inches is 15 centimetres

Can you write a program that does that?
You know how to read input with raw_input.
Do you understand how to use if/elif/else to make 
decisions inside your code?
You know how to do the basic calculations.
You know how to print the output.

> Maybe using if raw_input = feet then 
> do this equation. 

Exactly so.

> I am coding in pure Python on my mac, so 
> please nothing in C as  have been some 
> answers I have received.

I have no idea why anyone would suggest C for something 
like this, it is exactly the kind of program you would use 
Python for.

I don't know which book/tutorial you are using but the 
information you need for this problem can be found in 
the following topics of my tutor:
- Simple Sequences
- Talking to the User
- Branching

And optionally you may also want
- Loops
but they aren't strictly necessary for this.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list