2017-10-17 104 views
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() 
+0

你單獨編寫程序,並試圖將其重新導入爲模塊? –

回答

1

注:

  • 瞭解模塊和功能的區別。您不能導入的功能,在這種情況下inputRectcalcArea
  • ,因爲要爲每個進程創建功能,儘量利用return在你的函數來獲得您使用的功能需要
  • 還是在精神上數據,你可以分開一些計算。例如,而不是計算兩個矩形功能於一體,創造給予widthlength一個函數,只計算一個地區,

像這樣的東西可能是一個例子:

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) 
1

導入

您不必導入您正在使用的模塊中的函數(即:您啓動的文件的代碼)。

def inputRect(): 
    "Returns width and lenght in 2 tuples" 
    w1 = int(input("Width 1st rectangle: ")) 
    l1 = int(input("Lenght 1st rectangle: ")) 
    w2 = int(input("Width 2nd rectangle: ")) 
    l2 = int(input("Lenght 2nd rectangle: ")) 
    # tuple 1 (w & l of rect 1) - tuple 2 (w & l of r2) 
    return (w1, l1), (w2, l2) 

# get the 2 tuple into r1 and r2 to calculate the area 
r1, r2 = inputRect() 


def calcArea(): 
    "Uses the tuples to calculate area and returns results" 
    area1, area2 = r1[0] * r1[1], r2[0] * r2[1] 
    return area1, area2 

# This variable memorizes the two areas 
rect = calcArea() 


def difference(): 
    "Calculates which one area is bigger" 
    difference = rect[0] - rect[1] 
    # if ifference > 0 : rectangle 1 has a bigger area 
    if (difference) > 0: 
     print("Rectangle numer 1 is bigger than rectangle 2 by", rect[0] - rect[1]) 
    # if ifference < 0 : rectangle 2 has a bigger area 
    elif (difference) < 0: 
     print("Rectangle numer 2 is bigger than rectangle 1 by", rect[1] - rect[0]) 
    # else : both rectangles have the same area 
    else: 
     print("Both rectangles have the same area") 


difference() 

OUTPUT:

Width 1st rectangle: 10 
Lenght 1st rectangle: 10 
Width 2nd rectangle: 20 
Lenght 2nd rectangle: 20 
Rectangle numer 2 is bigger than rectangle 1 by 300 
相關問題