windows 下Python import 导入自定义模块

周末在家研究这个东西,则找到解决方案。

费话少说,上代码

 1 #定义一个自定义的函数,如下
 2 #函数的名称必须是字母和数字的组合,不能用数字开头
 3 #函数名后用小括号括住入参,可以用逗号分隔多个
 4 #如果有返回值用return ,如果没有返回值,默认返回None
 5 
 6 def PanDuanFenShu(score):
 7     if 100 >= score >= 90:
 8         print("A")
 9     if 90 > score >= 80:
10         print("B")
11     if 80 > score >= 60:
12         print("C")
13     if 60 > score >= 0:
14         print("D")
15     if score < 0 or score > 100:
16         print("输入错误")
17 
18 
19 def PanDuanFenShu2(score):
20     if 100 >= score >= 90:
21         return 'A'
22     if score >= 80:
23         return 'B'
24     if score >= 60:
25         return "C"
26     if 60 > score >= 0:
27         return 'D'
28     if score < 0 or score > 100:
29         return '输入错误'

调用时的代码

 1 #引入自定义模块
 2 import sys
 3 
 4 #注意自定义模块的路径字符串前有r
 5 sys.path.append(r"d:UsersAdminDesktoplianxi")
 6 
 7 #这句是必须的
 8 import fenshu
 9 
10 #调用前需要加文件名与小数点
11 fenshu.PanDuanFenShu(9)
原文地址:https://www.cnblogs.com/dhf327/p/4695721.html