Feet and inches

Rhodri James rhodri at wildebst.demon.co.uk
Wed Feb 4 21:01:12 EST 2009


On Thu, 05 Feb 2009 01:28:06 -0000, todpose at hotmail.com  
<todpose at hotmail.com> wrote:

> I'm trying to get Python to say:
> Enter the height (in metres):
> and convert whatever value to feet and inches. I've done this part as  
> you can see below, but [...]

Holy homework, you are busy tonight.  Hang on while I put the line breaks
back in your code.

> metres = float(raw_input("Enter the height (in metres): "))
> total_inches = 39.37 * metres
> feet = int(total_inches/12)
> inches = int(round(total_inches - feet*12))
> if feet>=1:
>    print "It is " +  str(feet) + " feet " + str(inches) + ' inches high.'
> if feet<1 and inches<0.5:
>    print "Error."

First things first: that line printing the result is making life harder
on yourself than you really need to, given that Python's print statement
(in 2.x -- it's a function from 3.0 onwards) will put spaces between items
for you, and will happily print out integers without needing to str() them.

     print "It is", feet, "feet", inches, "inches high."

Since the rest of this smells like homework, I'll answer your question with
questions.

> [...] how can I terminate the program when user inputs a height less
> than 1/2 inch?

First you have to make your program repeat itself, otherwise this question
is meaningless.  How would you do that?

Then, what methods do you have for stopping a loop in Python?  How would
you decide to make one of them happen?  What condition is "a height less
than 1/2 inch" in terms of your program?

(Hint: the condition isn't what I think you think it is -- remember that
"inches" is an int!)

> How can I also take into account all the cases that need an exception?

How do you take care of any exception?

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list