小白学Python(三) 几个简单的函数
来源:互联网 发布:怎么添加usb001端口 编辑:程序博客网 时间:2024/06/15 20:28
还是不太适应发博客……出去浪了一天就什么都不记得了囧rz
今天来更新第三篇。感觉都不知道应该写些什么了,于是就写几个弱弱的小函数开心一下吧~
首先是经典的辗转相除法:
def gcd(x, y):if(x%y == 0):return yreturn gcd(y, x%y)x = int(input("Please input a num\n>"))y = int(input("Please input another num\n>"))g = gcd(x,y)print("最大公约数" + str(g))print("最小公倍数" + str(x*y//g))
在这里提出一个提示。由于当年写下这段代码的时候对python还不怎么了解,但是写了这么多以后了解应该单独设立main函数,如下:
def gcd(x, y):if(x%y == 0):return yreturn gcd(y, x%y)def main():x = int(input("Please input a num\n>"))y = int(input("Please input another num\n>"))g = gcd(x,y)print("最大公约数" + str(g))print("最小公倍数" + str(x*y//g))if __name__ == '__main__':main()
希望和我一样的新手注意一下这个小问题。
一下的代码可能存在这样的问题,就懒得改了Orz
下面是计算斐波那契数列:
Fibonacci = []def getFibonacci(n):if len(Fibonacci) <= n:temp = getFibonacci(n-1) + getFibonacci(n-2)Fibonacci.append(temp)return Fibonacci[n]if __name__ == '__main__':Fibonacci.append(0)Fibonacci.append(1)Fibonacci.append(1)num = int(input("Please input an number:\n>"))print(str(getFibonacci(num)))
话说在现阶段以笔者的水平模拟一个数组还是蛮蛋疼的囧rz
判断是否是质数:
def isPrime(n):if n == 2:return Trueelif n < 2:return Falsefor i in range(2, n):if n/i == n//i:return Falsereturn Trueif __name__ == '__main__':num = int(input("Please enter a number:\n>"))print("Your number is " + str(num))print(str(isPrime(num)))
以及求因数:
def getFactors(n):factors = []for i in range(1,n+1):if n/i == n//i:factors.append(i)return factorsif __name__ == '__main__':num = int(input("Please enter a number:\n>"))print(getFactors(num))无话
格式化输出美元金额(四舍五入,加逗号):
class moneyFmt:def __init__(self, data = '0'):try:self.money = float(data)if self.money < 0:self.minus_sigh = Trueself.money *= (-1)else:self.minus_sigh = False#approximateself.money = (int(self.money*100))/100except TypeError:print("Initialize Error")self.money = 0def update(self, data):try:temp = float(data)self.money = tempexcept TypeError:print("Update error!")def __str__(self):#approximate#self.money = (int(self.money*100))/100f = self.money - int(self.money)inte = []money_copy = int(self.money)while money_copy != 0:inte.append(money_copy % 1000)money_copy //= 1000inte[0] += fstring = '$'if self.minus_sigh:string += '-'flag = Falsewhile (len(inte) > 0):if(flag):string += ','string += str(inte.pop())flag = Trueif string[-3] != '.':string += '0'return stringdef __nonzero__(self):if self.money == 0:return Falsereturn Truedef display(self):print(str(self))def main():while True:s = input("Please enter a number:\n>>>")S = moneyFmt(s)S.display()if __name__ == '__main__':main()
首先笔者在这段代码中没有写退出的判断,干脆一个异常拉倒,这样是不对滴~#但还是懒得改了Orz
还有就是这段代码笔者都觉得臃肿,有什么更好的改进办法欢迎指点~~~
就这样慢慢更下去吧,希望能够记录笔者的进步(~ ̄▽ ̄)~*
0 0
- 小白学Python(三) 几个简单的函数
- python-mysqldb的几个函数(一)
- python-mysqldb的几个函数(二)
- 几个简单的JS函数
- js的几个简单函数
- python re的几个函数
- 基础 python(三)关于string的几个tips
- 【python】python的几个内置函数
- Delphi 匿名函数的几个实际应用(三)
- python 实现libvirt管理(简单的几个功能)
- python 实现libvirt管理(简单的几个功能)
- 简单的几个shell脚本三
- 几个简单的函数示例(最大公约数、二进制转换、回文)
- python 的几个内置函数(lambda ,zip filter )用法
- Python 简单的几个小实例
- python几个简单的入门程序
- Python之几个简单的练习题
- 几个简单的jQuery扩展函数
- 【Java多线程】之五:wait, notify and notifyAll
- DOM创建表格
- 【转】一些图论、网络流入门题总结、汇总
- PRIM算法
- 回文数 找最长回文子串
- 小白学Python(三) 几个简单的函数
- 回文数 找第n个回文数
- Number of Islands
- Java中的50个关键字
- Java 两个方法彼此调用导致java.lang.StackOverflowError
- NOJ2101 7_密码强度(水模拟题)
- Android Studio轻松上手指南
- 迪卡斯特拉算法
- 使用PHP和HTML5 FormData实现无刷新文件上传教程