[Tutor] What is wrong my code?

Hoffmann oasf2004 at yahoo.com
Fri Mar 24 23:22:45 CET 2006


--- Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:

> 
> > leg1 = raw_input('Enter the first leg of the
> triangle:
> > ')
> > leg2 = raw_input('Enter the second leg of the
> > triangle: ')
> 
> Hi Hoffmann,
> 
> leg1 and leg2 here are strings, because raw_input()
> is guaranteed to
> return strings.
> 
> 
> You'll want to do something to turn those strings
> into numbers, since the
> hypotenuse() function:
> 
> > result = hypotenuse(leg1, leg2)
> 
> is going to break on non-numeric input.
> 
> 
> I think you'll find the float() function useful:
> it's a function that can
> take strings and return floating-point numbers.  For
> example:
> 
> ######
> >>> float("3.1415926")
> 3.1415926000000001
> >>> float("7")
> 7.0
> ######
> 
> 
> Good luck to you!
> 
> 

Hello Folks,

Thanks for the hints.
Please, see below, a new version. What do you think
about this new version?

Hoffmann
ps: The new version:

#!/usr/bin/python

import math
    
print '''This program calculates the lenght of the
hypotenuse of a right triangle
    given the lenghts of the two legs as
parameters.\n'''
    
leg1 = input('Enter the first leg of the triangle: ') 
leg2= input('Enter the second leg of the triangle: ') 
 

def hypotenuse(x, y):
    result = math.sqrt(x**2 + y**2)
    return result

result = hypotenuse(leg1, leg2)

print 'The hypotenuse of the triangle is', result

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list