我必须为学校编写一个程序,到目前为止,我有下面的代码,但是当我尝试运行它时,它说有一个错误,并且未定义compute_Area。
# 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.')
答案 0 :(得分:2)
正如usr2564301在评论中所说,您正在调用一个名为“ compute_area”的函数,但是您将该函数定义为compute_Area()
另外,如果要向函数中传递“ length1”和“ width1”,则需要在函数声明中声明(要传递2个变量)
答案 1 :(得分:1)
不是您问题的答案,而是对懒惰人的暗示;) 通常,如果您必须多次编写完全相同的行,则可以简化操作。
代替:
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 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.')
第二个功能完全相同。