我正在开发一个评估两个矩形面积的小程序。用户输入矩形的长和宽(我的第一个模块),然后程序计算矩形的面积(第二个模块),最后计算两个面积的差后,显示结果,告诉哪个是更大的。
但是输入长度和宽度后,程序显示错误信息,告诉我的模块没有定义为:
ImportError: No module named 'inputRect'
我的代码:
#Project M04: Rectangle with the bigger area
#Python 3.4.3
#Module that asks width and lenght of the two rectangles
def inputRect():
width1 = int(input("Enter the width of the first rectangle: "))
length1 = int(input("Enter the length of the first rectangle: "))
width2 = int(input("Enter the width of the second rectangle: "))
lenght2 = int(input("Enter the length of the second rectangle: "))
inputRect()
#import the fonction "inputRect"
import inputRect
#calcule the area of the two rectangles
def calcArea():
rect1 = int(width1) * int(length1)
rect2 = int(width2) * int(length2)
calcArea()
#import the fonction "calcArea"
import calcArea
#Calcul the difference between the two rectangles (rectangle 1 - rectangle 2 = difference)
#if > 0
def difference():
difference = int(rect1) - int(rect2)
# if ifference > 0 : rectangle 1 has a bigger area
if (difference) > 0 :
print ("Rectangle numer 1 is bigger than rectangle 2")
# if ifference < 0 : rectangle 2 has a bigger area
if (difference) < 0 :
print ("Rectangle numer 2 is bigger than rectangle 1")
# else : both rectangles have the same area
else:
print ("Both rectangles have the same area")
difference()
笔记:
inputRect
,并calcArea
return
在您的函数中使用来获取您需要的数据width
和length
这样的事情可能是一个例子:
def get_rect_input():
width1 = int(input("Enter the width of the first rectangle: "))
length1 = int(input("Enter the length of the first rectangle: "))
width2 = int(input("Enter the width of the second rectangle: "))
lenght2 = int(input("Enter the length of the second rectangle: "))
return width1, length1, width2, lenght2
def calculate_area(width, length):
return width * length
def show_comparation(width1, length1, width2, lenght2):
area1 = calculate_area(width1, lenght2)
area2 = calculate_area(width2, lenght2)
if area1 > area2:
print ("Rectangle number 1 is bigger than rectangle 2")
elif area1 < area2:
print ("Rectangle number 2 is bigger than rectangle 1")
else:
print ("Both rectangles have the same area")
if __name__ == "__main__":
width1, lenght1, width2, lenght2 = get_rect_input()
show_comparation(width1, lenght1, width2, lenght2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句