python的模块代码调用
来源:互联网 发布:api python 编辑:程序博客网 时间:2024/06/13 23:39
一、模块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 gcdprint "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 integersn1 = 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 divisorenter the first number: 45enter the second number: 75the divisor for 45 and 75 is 15
2. 方法2:直接导入模块GCDFunction.py
import GCDFunctionn1 = 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 divisorenter the first number: 45enter the second number: 75the divisor for 45 and 75 is 15
三、下面,运用模块GCDFunction.py,实现功能:输入两个整数,求它们的最大公约数,循环做3次
1. 方法1:从模块GCDFunction.py导入函数gcd
from GCDFunction import gcdfor 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 divisorenter the first number: 15enter the second number: 75the divisor for 15 and 75 is 15enter the first number: 16enter the second number: 48the divisor for 16 and 48 is 16enter the first number: 17enter the second number: 51the divisor for 17 and 51 is 17
2. 方法2:直接导入模块GCDFunction.py
import GCDFunctionfor 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 divisorenter the first number: 15enter the second number: 75the divisor for 15 and 75 is 15enter the first number: 16enter the second number: 48the divisor for 16 and 48 is 16enter the first number: 17enter the second number: 51the divisor for 17 and 51 is 17
阅读全文
0 0
- python的模块代码调用
- Python media 模块的调用
- 一段用Python语言的Ctypes模块调用系统api函数写的rsa加密代码
- python调用C模块的方法
- python调用父目录的模块
- Python模块的编写和调用
- python调用自编模块的方法
- 在python中调用grass的模块
- Python包间模块的互相调用
- python 动态调用模块内的函数
- python 调用自定义的模块函数
- 简单的logging模块调用(python)
- 【机器学习】python第三方模块lda包调用代码
- Python调用C的代码
- python入门笔记--模块和模块包的调用
- python模块调用心得体会
- python调用自定义模块
- C#调用Python模块
- mybatis的动态SQL(二)choose标签的使用
- 57.笔记go语言——闭包
- Ajax
- Hive学习笔记 4 Hive的数据导入
- HttpURLConnection、HttpClient网络请求
- python的模块代码调用
- 58.笔记go语言——函数式编程
- 自定义控件
- 蓝牙扫描 权限 BLUETOOTH,BLUETOOTH_ADMIN
- POJ 3262
- 死锁
- linux任务计划几个小命令
- 如何搭建自己的shadowsocks
- 59.笔记go语言——go语言中的反射