【问题标题】:how to calculate surface area of a pyramid in python?如何在python中计算金字塔的表面积?
【发布时间】:2020-07-02 20:00:18
【问题描述】:

如何使用下面的公式在python中计算金字塔的表面积?

面积 = 底^2 + 底√(底^2 + 4 ∙ 高度^2)

import math
def main():

    # Your main code goes here
    b = eval(input("Enter the base: "))
    h = eval(input("Enter the height: "))

    print("The surface area of the pyramid is : ",Area)


# Create a method here to calculate pyramid area

# ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓


import math

def calcPyramid(b,h):

    Area = math.pow(b * b + b (math.sqrt(b * b + 4 * h * h)))
    return Area

##########################################################
# This will run your main method when we load the project.
# Do not change this `if` statement or your program will fail all tests on 
Gradescope!
###########################################################
if __name__ == '__main__':
    main()

【问题讨论】:

  • 在您的计算中,您有:b * b + b (...)。对于 python,b(...) 看起来像一个函数调用,您期望 b 是函数的名称。您的意思是:b * b + b * ( ... )

标签: python methods computer-science


【解决方案1】:

您必须使用* 运算符来表示乘法(在您的情况下,b 乘以平方根)。

【讨论】:

    【解决方案2】:
    # Python 3 program to find the 
    # surface area Of Square pyramid 
    
    # function to find the surface area 
    def surfaceArea(b, s): 
    
        return 2 * b * s + pow(b, 2) 
    
    # Driver Code 
    if __name__ == "__main__": 
        b = 3
        s = 4
    
        # surface area of the square pyramid 
        print(surfaceArea(b, s)) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      相关资源
      最近更新 更多