《Python编程快速上手》(三)Python函数

第三章   函数

3.1 def语句和参数

def hello():    #def语句,定义了一个名为hello()的函数
    print('Howdy!')          #函数体
    print('Howdy!!!')        #函数体
    print('Hello there!')    #函数体
hello()     #调用
hello()     #调用
hello()     #调用
  • 代码在函数调用时被执行,而不是在函数第一次定义时执行

  • 函数的一个目的就是将需要多次执行的代码放在一起,避免复制代码

def hello(name):    #name为hello()函数的参数
    print('Hello '+ name)
hello('Saber')
hello('Arther')

  • 保存在变元中的值,在函数返回后就被丢失了

3.2 返回值和 return 语句

  • 一般来说,函数调用求值的结果,称为函数的“返回值
  • return语句包括:①return关键字;②函数应该返回的值或表达式

3.3 None值

  • None是NoneType数据类型的唯一值
  • 相当于C语言函数返回值为void
  • 对于没有return语句的函数,Python会在末尾加上return None(类似 while或for循环隐式的以continue结尾),如果使用不带值的return语句也返回None

3.4 关键字参数和print()

【例】print()函数中的end,sep

print('Hello')
print('World')

  • print()函数自动在传入的字符串结尾添加了换行符
print('Hello',end='')
print('World')


3.5 局部和全局作用域

3.5.1 局部变量不能在全局作用域内使用

def spam():
    eggs = 65535
spam()
print(eggs)

  • 函数spam()调用完成后,eggs(局部变量)被销毁

3.5.2 局部作用域不能使用其他局部作用域内的变量

def spam():
    eggs = 65535
    bacon()
    print(eggs)
def bacon():
    ham = 100
    eggs = 0
spam()

3.5.3 全局变量可以在局部作用域中读取

def spam():
    print(eggs)
eggs = 2
spam()
print(eggs)

3.5.4 名称相同的局部变量和全局变量

def spam():
    eggs = 'spam local'
    print(eggs)
def bacon():
    eggs = 'bacon local'
    print(eggs)
    spam()
    print(eggs)
eggs = 'global'
bacon()
print(eggs)


3.6 global语句

  • 在一个函数内修改全局变量
def spam():
    global eggs
    eggs = 'spam'
eggs = 'global'
spam()
print(eggs)

【4条法则区分一个变量是处于局部作用域还是全局作用域】

  • 若变量在全局作用域使用(即在任何函数之外),它就总是全局变量
  • 若在一个函数中,有针对该变量的global语句,它就是全局变量
  • 否则,若该变量用于函数中的赋值语句,它就是局部变量
  • 但是,若该变量没有用在赋值语句中,它就是全局变量
def spam():
    global eggs
    eggs = 'spam'   #全局变量
    print(eggs)
def bacon():
    eggs = 'bacan'  #局部变量
    print(eggs)    
def ham():
    print(eggs)     #全局变量   
eggs = 'global'
spam()
bacon()
ham()

  • 在一个函数中,一个变量要么总是全局变量,要么总是局部变量

3.7 异常处理

  • try子句(可能出错的语句)    except子句(错误发生,执行的语句)

def spam(divideBy):
    try:
        return 42/divideBy
    except ZeroDivisionError:
        print('Error:Invalid argument')
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(3))

  • None的输出是因为print()函数的返回值为none

3.8 一个小程序:猜数字

#猜数字
import random
digit=random.randint(1,20)
for i in range(1,7):
    print('input a num')
    num=int(input())
    if num==digit:
        break
    elif num>digit:
        print('too hight')
    elif num<digit:
        print('too low')
if num==digit:
    print('right!count='+str(i))
else:
    print('None'+'right answer is'+str(digit))

3.9 小结

3.10 习题


3.11 实践项目

3.11.1 Collate序列

3.11.2 输入验证

#Collatz序列
def clooatz(number):
    if number%2==0:
        print(str(number//2))
        return number//2
    else:
        print(str(3*number+1))
        return 3*number+1
try:
    number = int(input())
    while number!=1:
        number=clooatz(number)
except ValueError:
    print('your input must be intger!')

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值