python学习笔记5―函数

函数定义:

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


你可能感兴趣的:(python,学习,笔记)