我想写一个简单的代码,用公式找到三角形的面积:A =(1/2)b(h)其中b是基数,h是高度。如何让用户输入2个输入,b和h?
答案 0 :(得分:1)
在Python 3.x中,您可以使用input()函数获取输入。它将打印一个字符串,然后从用户那里获取输入。我还添加了一小段代码,显示如何将其转换为浮点数,然后进行数学运算。
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)))
在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)))
答案 1 :(得分:1)
在Python 3.x中:
base = input('Enter base: ')
height = input('Enter height: ')
请注意,base
和height
是字符串。因此,在计算之前将其转换为int
或float
。
在Python 2.x中:
base = raw_input('Enter base: ')
height = raw_input('Enter height: ')