创建自己的模块

我是Python新手。请帮我解决这个简单的问题


在练习中,我们已经有了模块中的主程序,如下:


    

# -*- coding: cp1252 -*-


import mymodule


mymodule.printme("Exampleline")

目标是实现练习中应用的 mymodule-module。创建一个模块,它有一个 printme 函数,它打印给定的参数,并带有免责声明“我得到:”,然后是“参数的长度为 [length] 个字符”。当模块正确实现时,程序会打印出以下内容:


>>> 


I got: Exampleline

The parameter was 11 characters long.

>>> 

输出示例:


I got: Exampleline

The parameter was 11 characters long.

这是我的输出:


import mymodule

def main():

    n=mymodule.printme

    m=len(n)

    print("I got: ",n)

    print("The parameter was ",m,"characters long.")

if __name__=="__main__":

    main()

老实说,我不太明白这项练习的要求是什么。因此,我需要大家的帮助。以下是翻译报告:


文件“ohjelma.py”,第 5 行,在 mymodule.printme(“Exampleline”) 中 AttributeError:模块“mymodule”没有属性“printme”



胡说叔叔
浏览 118回答 3
3回答

沧海一幻觉

您可以非常轻松地创建模块。这只是另一个文件。最简单的方法是创建 mymodule.py 文件。在 mymodule.py 文件中,您将需要一个函数。# mymodule.pydef printme(string):    print('I got: {0}'.format(string))    print('The parameter was {} characters long.'.format(len(string))然后您可以像示例中一样导入它,并运行该printme函数。

开心每一天1111

在该练习中,您将在 INSIDE mymodule.py 中编写。您的目标是在该 mymodule 中编写 printme 函数,以便当主模块导入该 mymodule 并调用函数 printme 时,会生成示例输出。这对我有用:def printme(text):    print("I got:",text)    print("The parameter was",len(text),"characters long.")

浮云间

def printme(sth):    print("I got:",(sth))    print("The parameter was",len(sth),"characters long.")试试这个,它有效
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python