[英]Program to calculate and compare the area of two rectangles
I have to write a program for school and so far I have the code below but when I try to run it it says there was an error and that compute_Area was not defined. 我必须为学校编写一个程序,到目前为止,我有下面的代码,但是当我尝试运行它时,它说有一个错误,并且未定义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.')
You're calling a function called compute_area
but you defined the function as compute_Area()
. 您正在调用一个称为
compute_area
的函数,但已将该函数定义为compute_Area()
。
Also if you're passing in length1
and width1
into the function, you need to declare in your function declaration that you're passing in 2 variables. 此外,如果你正在传递
length1
和width1
到功能,您需要在您传递在2个变量的函数声明来声明。
Not an answer to your question but a hint for lazy people ;) Often if you have to write the exact same line many times, it can be done simpler.. 不是您的问题的答案,而是给懒惰的人的提示;)通常,如果您必须多次写完全相同的一行,则可以更简单地完成。
instead of: 代替:
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.')
you could also just write: 您也可以这样写:
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.')
The second function is doing exactly the same. 第二个功能做的完全一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.