python学习笔记4—函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习笔记4—函数相关的知识,希望对你有一定的参考价值。
函数定义:
def fun()
[[email protected] ~]# vim fun.py
#!/usr/bin/python
def fun():
sth = raw_input("Please input something")
try:
if type(int(sth))==type(1):
print "%s is a number" %sth
except ValueError:
print "%s is not number" %sth
fun()
[[email protected] ~]# python fun.py
Please input something34
34 is a number
[[email protected] ~]# python fun.py
Please input somethingdf
df is not number
python传递参数
[[email protected] ~]# vim isNum.py
#!/usr/bin/python
import sys
def isNum(s):
for i in s:
if i in ‘0123456789‘:
pass
else:
print "%s is not a number" %s
sys.exit()
else:
print "%s is a number" % s
isNum(sys.argv[1]) #argv[1]指12或者abc,argv[0]指isNum.py
[[email protected] ~]# python isNum.py 12
12 is a number
[[email protected] ~]# python isNum.py abc
abc is not a number
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1774533
以上是关于python学习笔记4—函数的主要内容,如果未能解决你的问题,请参考以下文章