码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记4—函数

时间:2016-05-18 06:58:09      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:python学习笔记4—函数

函数定义:

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

python学习笔记4—函数

标签:python学习笔记4—函数

原文地址:http://daixuan.blog.51cto.com/5426657/1774533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!