首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python 3:导入模块

python 3:导入模块
EN

Stack Overflow用户
提问于 2017-10-17 19:55:29
回答 2查看 215关注 0票数 0

我正在做一个小程序来评估两个矩形的面积。用户输入矩形的长度和宽度(我的第一个模块),然后程序计算矩形的面积(第二个模块),最后在计算两个区域之间的差值后,显示结果,告诉哪个是较大的。

但是,输入长度和宽度后,程序将显示一条错误消息,告知我的模块未定义为:

代码语言:javascript
复制
ImportError: No module named 'inputRect'

我的守则:

代码语言:javascript
复制
#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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-17 20:45:11

备注:

  • 了解模块和功能之间的区别。不能导入函数,在本例中是inputRectcalcArea
  • 由于您希望为每个进程创建函数,所以尝试在您的函数中使用return来获取所需的数据。
  • 仍然在使用函数的精神,你可以分开一些计算。例如,与其在一个函数中计算两个矩形,不如创建一个只计算面积的函数,给定widthlength

像这样的事情可以作为一个例子:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-10-19 07:26:01

正在导入

您不必导入正在使用的模块中的函数(即启动文件的代码)。

代码语言:javascript
复制
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()

产出: 宽1矩形: 10长1矩形: 10宽第2矩形: 20长2矩形: 20长方形数字2比长方形1大300

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46798185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档