# A function to calculate area of triangle (1/2 base times height) def areaOfTriangle( base, height ): area = 0.5 * base * height return area ##### Main execution area of your program. # Ask user for base and height, store in variables base = raw_input("Enter triangle base length: ") height = raw_input("Enter triangle height: ") # The numbers entered are still just keyboard characters # We need to cast them to floats (numbers with decimal points) base = float( base ) height = float( height ) # Pass our base and height variables to the function print 'Area of triangle is', areaOfTriangle( base, height ) # We can also store the return value in a variable # and use it however we want. area = areaOfTriangle( base, height ) print 'The area of the triangle is', area
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question