函数定义:
def fun()
[root@localhost ~]# 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()
[root@localhost ~]# python fun.py
Please input something34
34 is a number
[root@localhost ~]# python fun.py
Please input somethingdf
df is not number
python传递参数
[root@localhost ~]# 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
[root@localhost ~]# python isNum.py 12
12 is a number
[root@localhost ~]# python isNum.py abc
abc is not a number
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1774533
原文地址:http://daixuan.blog.51cto.com/5426657/1774533