python的模块代码调用
2017-10-08 20:18
288 查看
一、模块GCDFunction.py,用来求两个数的最大公约数
二、下面,运用模块GCDFunction.py,实现功能:输入两个整数,求它们的最大公约数
1. 方法1:从模块GCDFunction.py导入函数gcd
结果为:
2. 方法2:直接导入模块GCDFunction.py
三、下面,运用模块GCDFunction.py,实现功能:输入两个整数,求它们的最大公约数,循环做3次
1. 方法1:从模块GCDFunction.py导入函数gcd
2. 方法2:直接导入模块GCDFunction.py
def gcd(n1,n2): gcd=1 k=2 while k<=n1 and k<=n2: if n1%k==0 and n2%k==0: gcd=k k+=1 return gcd print "Finishing calculating greatest common divisor"
二、下面,运用模块GCDFunction.py,实现功能:输入两个整数,求它们的最大公约数
1. 方法1:从模块GCDFunction.py导入函数gcd
from GCDFunction import gcd # Import the module # Prompt the user to enter two integers n1 = eval(input("Enter the first integer: ")) n2 = eval(input("Enter the second integer: ")) print("The greatest common divisor for", n1, "and", n2, "is", gcd(n1, n2))
结果为:
Finishing calculating greatest common divisor enter the first number: 45 enter the second number: 75 the divisor for 45 and 75 is 15
2. 方法2:直接导入模块GCDFunction.py
import GCDFunction n1 = eval(raw_input("enter the first number: ")) n2 = eval(raw_input("enter the second number: ")) n1n2gcd = GCDFunction.gcd(n1,n2) print "the divisor for %i and %i is %i" %(n1,n2,n1n2gcd)结果为:
Finishing calculating greatest common divisor enter the first number: 45 enter the second number: 75 the divisor for 45 and 75 is 15
三、下面,运用模块GCDFunction.py,实现功能:输入两个整数,求它们的最大公约数,循环做3次
1. 方法1:从模块GCDFunction.py导入函数gcd
from GCDFunction import gcd for i in range(3): n1 = eval(raw_input("enter the first number: ")) n2 = eval(raw_input("enter the second number: ")) print "the divisor for %i and %i is %i" %(n1,n2,gcd(n1,n2)) print结果为:
Finishing calculating greatest common divisor enter the first number: 15 enter the second number: 75 the divisor for 15 and 75 is 15 enter the first number: 16 enter the second number: 48 the divisor for 16 and 48 is 16 enter the first number: 17 enter the second number: 51 the divisor for 17 and 51 is 17
2. 方法2:直接导入模块GCDFunction.py
import GCDFunction for i in range(3): n1 = eval(raw_input("enter the first number: ")) n2 = eval(raw_input("enter the second number: ")) n1n2gcd = GCDFunction.gcd(n1,n2) print "the divisor for %i and %i is %i" %(n1,n2,n1n2gcd) print结果为:
Finishing calculating greatest common divisor enter the first number: 15 enter the second number: 75 the divisor for 15 and 75 is 15 enter the first number: 16 enter the second number: 48 the divisor for 16 and 48 is 16 enter the first number: 17 enter the second number: 51 the divisor for 17 and 51 is 17
相关文章推荐
- Python3基础 创建一个模块 导入并调用其中的函数 主代码与模块代码在同一文件夹下
- 解决python模块调用时代码中使用相对路径访问的文件,提示文件不存在的问题
- 一段用Python语言的Ctypes模块调用系统api函数写的rsa加密代码
- 【机器学习】python第三方模块lda包调用代码
- C#调用Python脚本并使用Python的第三方模块
- 如何调用你所分享的python代码
- 使用python调用mysql模块插入数据
- python调用Java代码并执行--------jpype使用篇
- python 调用不同目录里的 包package 或者 模块module
- python学习(1):__name__变量在模块调用时所起的作用
- Python接口(2):使用ctpyes模块在python中调用c++代码
- Python 代码模块八股文
- python之模块之间相互调用
- Python实战小程序利用matplotlib模块画图代码分享
- Python 引用多个模块,调用模块中的函数时...要注意的地方
- windows下安装python paramiko模块的代码
- Python使用Turtle模块绘制五星红旗代码示例
- Windows C++程序调用包含TensorFlow代码的Python脚本出错问题的解决办法
- 高效开发Python扩展模块方法:用SWIG封装C/C++代码
- python使用ctypes模块调用windowsapi获取系统版本示例