函数定义:
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
时间: 2024-10-14 00:51:19