三角形python的面积/类型

时间:2021-03-07 03:07:50

标签: python area

我应该找到一个代码,询问用户三角形的底边、高度和边,然后告诉用户“这个(类型)三角形的面积为(面积)”,然后不断询问用户更多的号码,直到他们决定退出。我到目前为止的代码是...

again = "y"
while again != "n":

base = int(input("Enter base: "))  
height = int(input("Enter height: "))  
side1 = int(input("Enter side 1:  "))
side2 = int(input("Enter side 2:  "))
side3 = int(input("Enter side 3:  "))
area = (base*height) / 2

certain_type = []
if side1 == side2 == side3:
        print("Equilateral triangle")
    elif side1==side2 or side2==side3 or side1==side3:
        print("isosceles triangle")
    else:
        print("Scalene triangle")


print('This %f of the triangle is %0.2f' %area, certaintype)

1 个答案:

答案 0 :(得分:-1)

干得好,所以一些评论......

(1) 缩进把它扔掉,因为整个代码应该在 while 循环下,这样你的整个游戏就会在每个三角形之后重复自己

(2) 不是直接打印“isosceles”、“scalene”等,而是“certaintype”……您可以使用certaintype='scalene' 将值(例如scalene)存储在特定类型变量下。这样,您可以稍后在句子中打印三角形的类型。

(3) 最终打印也应该在 while 循环下,因为您想在每次输入三角形信息后告诉用户答案。

(4) 字符串插值的语法对于 certaintype 这样的字符串变量是 %s,而对于数字变量(比如 area),数字/小数的语法是 %d。看看我是如何重写最终打印行的。

这样的事情应该可行:

while again != "n":

    base = int(input("Enter base: "))
    height = int(input("Enter height: "))
    side1 = int(input("Enter side 1:  "))
    side2 = int(input("Enter side 2:  "))
    side3 = int(input("Enter side 3:  "))
    area = (base*height) / 2

    certain_type = []
    if side1 == side2 == side3:
        certaintype = "Equilateral triangle"
    elif side1==side2 or side2==side3 or side1==side3:
        certaintype = "isosceles triangle"
    else:
        certaintype = "Scalene triangle"


    print('This %d of the triangle is %s' % (certaintype, area) )