【问题标题】:Area/Type of triangle python三角蟒的面积/类型
【发布时间】:2021-06-05 07:57:11
【问题描述】:

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

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)

【问题讨论】:

  • 您的具体问题是什么?
  • 干得好,所以有几个 cmets... 1) 缩进会把它扔掉,因为整个代码应该在 while 循环下。
  • 这里有一个错字: certaintype 和 certain_type ,这个列表仍然是空的。你想在里面添加什么?

标签: python area


【解决方案1】:

干得好,有几个cmets...

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

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

(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) )

【讨论】:

  • 我对你的问题投了反对票。您似乎所做的是代码审查。但是代码审查不是我们在这里提供的服务,可以在我们的兄弟网站Code Review 提出请求。
  • @KlausD。这是一个答案,而不是一个问题。
  • 确实如此。但这个小错误不会使消息无效。
猜你喜欢
  • 2014-11-03
  • 1970-01-01
  • 2021-12-21
  • 2014-12-06
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 2015-10-15
相关资源
最近更新 更多