Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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_Python 3.x - Fatal编程技术网

Python 计算和比较两个矩形面积的程序

Python 计算和比较两个矩形面积的程序,python,python-3.x,Python,Python 3.x,我必须为学校写一个程序,到目前为止,我有下面的代码,但当我尝试运行它时,它说有一个错误,计算区域没有定义 # This program will calculate and compare the areas of # two rectangles and display which is the greater area def main(): length1 = int(input('Length of rectangle one: ')) width1 = int(inpu

我必须为学校写一个程序,到目前为止,我有下面的代码,但当我尝试运行它时,它说有一个错误,计算区域没有定义

# This program will calculate and compare the areas of
# two rectangles and display which is the greater area
def main():
    length1 = int(input('Length of rectangle one: '))
    width1 = int(input('Width of rectangle one: '))
    area1 = compute_area(length1, width1)
    length2 = int(input('Length of rectangle two: '))
    width2 = int(input('Width of rectangle two: '))
    area2 = compute_Area(length2, width2)
    comparison_Area(area1, area2)
main()

def compute_Area():
    area = length * width
    return area

def comparison_Area():
    if area1 > area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle one\'s area is greater.')
    elif area1 < area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle two\'s area is greater.')
    elif area1 == area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle\'s areas are equal.')
#此程序将计算和比较
#两个矩形并显示哪个区域更大
def main():
length1=int(输入('矩形一的长度:'))
宽度1=int(输入('矩形1的宽度:'))
面积1=计算面积(长度1,宽度1)
length2=int(输入('矩形2的长度:'))
宽度2=int(输入('矩形2的宽度:'))
面积2=计算面积(长度2,宽度2)
比较区域(区域1、区域2)
main()
def compute_Area():
面积=长度*宽度
返回区
def比较_区域():
如果区域1>区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形的面积更大')
elif区域1<区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形二的面积更大')
elif区域1==区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形的面积相等')

您正在调用一个名为
compute\u area
的函数,但您将该函数定义为
compute\u area()


另外,如果要将
length1
width1
传递到函数中,则需要在函数声明中声明要传递2个变量。

调用名为
compute\u area
的函数,但将函数定义为
compute\u area()


另外,如果要将
length1
width1
传递到函数中,则需要在函数声明中声明要传递2个变量。

不是对问题的回答,而是对懒惰的人的提示;) 通常情况下,如果你必须多次写同一行,可以做得更简单

而不是:

def comparison_area():
    if area1 > area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle one\'s area is greater.')
    elif area1 < area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle two\'s area is greater.')
    elif area1 == area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle\'s areas are equal.')
def比较_区域():
如果区域1>区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形的面积更大')
elif区域1<区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形二的面积更大')
elif区域1==区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形的面积相等')
你也可以写:

def comparison_area():
    print()
    print('The area of rectangle one is: ', area1)
    print('The area of rectangle two is: ', area2)
    if area1 > area2:
        print('Rectangle one\'s area is greater.')
    elif area1 < area2:
        print('Rectangle two\'s area is greater.')
    elif area1 == area2:
        print('Rectangle\'s areas are equal.')
def比较_区域():
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
如果区域1>区域2:
打印('矩形的面积更大')
elif区域1<区域2:
打印('矩形二的面积更大')
elif区域1==区域2:
打印('矩形的面积相等')

第二个功能与此完全相同。

不是对您的问题的回答,而是对懒人的提示;) 通常情况下,如果你必须多次写同一行,可以做得更简单

而不是:

def comparison_area():
    if area1 > area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle one\'s area is greater.')
    elif area1 < area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle two\'s area is greater.')
    elif area1 == area2:
        print()
        print('The area of rectangle one is: ', area1)
        print('The area of rectangle two is: ', area2)
        print('Rectangle\'s areas are equal.')
def比较_区域():
如果区域1>区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形的面积更大')
elif区域1<区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形二的面积更大')
elif区域1==区域2:
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
打印('矩形的面积相等')
你也可以写:

def comparison_area():
    print()
    print('The area of rectangle one is: ', area1)
    print('The area of rectangle two is: ', area2)
    if area1 > area2:
        print('Rectangle one\'s area is greater.')
    elif area1 < area2:
        print('Rectangle two\'s area is greater.')
    elif area1 == area2:
        print('Rectangle\'s areas are equal.')
def比较_区域():
打印()
打印('矩形1的面积为:',区域1)
打印('矩形2的面积为:',区域2)
如果区域1>区域2:
打印('矩形的面积更大')
elif区域1<区域2:
打印('矩形二的面积更大')
elif区域1==区域2:
打印('矩形的面积相等')

第二个函数的作用完全相同。

对于初学者,将
main
函数移到脚本的末尾。您正在调用一个只在以后声明的函数
php
允许您这样做,但不是
python
。对于初学者,请将
main
函数移到脚本的末尾。您正在调用一个只在以后声明的函数
php
允许您这样做,但不能
python