0
我正在研究一個評估兩個矩形區域的小程序。用戶輸入矩形的長度和寬度(我的第一個模塊),然後程序計算矩形的面積(第二個模塊),最後在計算兩個區域之間的差值後,顯示結果,告訴哪一個是更大的。python 3:import module
但是進入的長度和寬度之後,程序將顯示一個錯誤消息,告訴我的模塊沒有被定義爲:
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()
你單獨編寫程序,並試圖將其重新導入爲模塊? –