Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试计算给定宽度和对角线的矩形面积_Python - Fatal编程技术网

Python 尝试计算给定宽度和对角线的矩形面积

Python 尝试计算给定宽度和对角线的矩形面积,python,Python,我知道我并没有在某个地方定义对角线,但到目前为止我已经做到了: import math def compute_height_rectangle(width,diagonal): height=area/diagonal return height height = int(input("Please enter the length of the diagonal: ")) "height = int(height) " width = int(input("please

我知道我并没有在某个地方定义对角线,但到目前为止我已经做到了:

import math

def compute_height_rectangle(width,diagonal):
    height=area/diagonal
    return height

height = int(input("Please enter the length of the diagonal: "))
"height = int(height) "
width  = int(input("please enter the width: "))
"diagonal = width"

def compute_area_rectangle(width,diagonal):
    area=height*width
    return area

print(compute_area_rectangle(width,diagonal))
使用,我们注意到:

对角线^2=高度^2+宽度^2非python代码

知道对角线总是大于高度,我们有:

高度=平方米对角线^2-宽度^2

因此,您需要的代码是:

def compute_height_rectangle(width, diagonal):
    return (diagonal ** 2 - width ** 2) ** 0.5

def compute_area_rectangle(width, diaognal):
    return width * compute_height_rectangle(width, diagonal)

对角线从未定义,因为您具有

"diagonal = width"
在双引号中,这使得它只是一个字符串。我复制了你的代码并删除了双引号,它没有产生错误。我亦会删除:

"height = int(height)"
行,因为不需要将高度指定为整数输入


对于将来的问题,如果您告诉我们您试图使代码执行的操作,那么这将有所帮助,这样我们也可以发现任何其他错误,并且这可能会为您节省一些精力:。

在代码中交换高度和对角线时,您可能需要清理变量。这让你很难理解你的真正意思。从头开始,在纸上写出计算面积所需的每一步。然后将该流转换为代码。尽量将def组织在一个地方,而不是全部,因为这有助于提高可读性