-2

I want to write a simple code to find the area of a triangle using the formula: A=(1/2)b(h) where b is the base and h is the hieght. How can I ask the user to enter 2 inputs, b and h?

  • Still answered the question, go ahead and look at it. I even covered how to convert variables into floats (as that is probably what you want). If you could do me a huge favor and mark it was solution and upvote it that'd be great. – Neil Mar 10 '17 at 03:59

2 Answers2

1

In Python 3.x you can take input using the input() function. It will print a string and then take input from the user. I also added a small line of code showing how to convert that into a float and then do the mathematics.

input1 = input("What would you like your base of the triangle to be? ")
input2 = input("What would you like your height of the triangle to be? ")
print ("Your result " + str(.5 * float(input1) * float(input2)))

In Python 2.x

input1 = raw_input("What would you like your base of the triangle to be? ")
input2 = raw_input("What would you like your height of the triangle to be? ")
print ("Your result " + str(.5 * float(input1) * float(input2)))
Neil
  • 14,063
  • 3
  • 30
  • 51
0

In Python 3.x:

base = input('Enter base: ')
height = input('Enter height: ')

Please note, base and height is string here. So, convert it to int or float before computation.

In Python 2.x:

base = raw_input('Enter base: ')
height = raw_input('Enter height: ')
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161