第三章 函数【python编程快速上手】

 

# 3.3 None值
# 一个使用None的地方就是print()函数的返回值
spam = print('hello')
print(spam)    # None

# 3.4 关键字参数和print()函数
## print()默认以换行符为结尾,如果有指定就按照指定的~~为结尾
## end关键字参数:以~~作为结尾
print('hello',end='')
print('world!')          #helloworld!

print('hello',end=' ')
print('world!')          #hello world!

## 如果向print()函数传入多个字符串值,该函数就会启动一个空格分隔它们
## 也可以传入sep关键字参数,替换掉默认的分隔字符串
print('cat','dog','pig')                #cat dog pig
print('cat','dog','pig',sep=',')        #cat,dog,pig
print('cat','dog','pig',sep='-')        #cat-dog-pig


# 3.5 调用栈
def a():
    print('a() starts')
    b()
    d()
    print('a() returns')

def b():
    print('b() starts')
    c()
    print('b() returns')

def c():
    print('c() starts')
    print('c() returns')

def d():
    print('d() starts')
    print('d() returns')

a()

#3.6.2 全局作用域不能使用其他局部作用域内的变量
def spam():
    eggs=90
    bacon()
    print(eggs)

def bacon():
    eggs=0    #在bacon()局部变量eggs 与 spam()局部作用域中的那个变量不同

spam()    # 90---->

#3.6.2 全局变量可以在局部作用域中读取
def spam():
    print(eggs)    #42
eggs=42
spam()
print(eggs)        #42

# 3.6.4 名称相同的局部变量和全局变量
def spam():
    eggs = 'spam local'
    print(eggs)           # prints 'spam local'

def bacon():
    eggs = 'bacon local'
    print(eggs)           # prints 'bacon local'
    spam()
    print(eggs)           # prints 'bacon local'

eggs = 'global'   #全局变量
bacon()
print(eggs) # prints 'global'


# 3.7 global语句
## 3.7.1
def spam():
    global eggs
    eggs = 'spam'

eggs = 'global'
spam()
print(eggs)    #spam

## 3.7.2
def spam():
    # global eggs
    eggs = 'spam'

eggs = 'global'
spam()
print(eggs)      #global

## 3.7.3
def spam():
    global eggs
    eggs = 'spam'        # prints 'spam local'

def bacon():
    eggs = 'bacon'

def ham():
    print(eggs)  # 在ham()函数没有对eggs赋值,所以该处的eggs为全局变量

eggs = 42   #全局变量
spam()
print(eggs)   #'spam'

## 3.7.44
# def spam():
#     print(eggs) # ERROR!
#     eggs = 'spam local'
# # 在spam()函数对eggs赋值,所以该处的eggs为局部局变量
# #但是print(eggs)在执行eggs之前,所以局部变量并不存在
#
# eggs = 'global'
# spam()

# 修改方法1:
def spam():
    print(eggs)   #eggs为全局变量,不会报错

eggs = 'global'
spam()      #'global'

# 修改方法2:
def spam():
    eggs = 'spam local'
    print(eggs)   #eggs为局部变量,不会报错

eggs = 'global'
spam()      #'spam local'


# 3.8 异常处理

# def spam(divideBy):
#     return 42 / divideBy
#
#     print(spam(2))
#     print(spam(12))
#     print(spam(0))   #"除数为0"的错误
#     print(spam(1))
#     print('Error: Invalid argument.')

# 修改方法1:
def spam(divideBy):
    try:
        return 42 / divideBy

    except ZeroDivisionError:
        print('Error: Invalid argument.')

print(spam(2))
print(spam(12))
print(spam(0))
# Error: Invalid argument.
# None   代表返回值是一个空值
print(spam(1))      #42.0

# 修改方法2:调用不变,函数部分用try来实现
def spam(divideBy):
    return 42 / divideBy

try:
    print(spam(2))
    print(spam(12))
    print(spam(0))
    print(spam(1))
except ZeroDivisionError:
    print('Error: Invalid argument.')
# 21.0
# 3.5
# Error: Invalid argument.----->当执行到‘除数为0’发生异常的时候,直接执行except语句,后边try语句就没有再执行

# 3.9 小程序:Zizag
import time, sys
indent = 0                      # How many spaces to indent. 前边要有几个空格
indentIncreasing = True         # Whether the indentation is increasing or not.  indentIncreasing决定递增还是递减

try:
    while True:                     # The main program loop.
        print(' ' * indent, end='')   #打印多少个空格,end=''代表结束不用换行符了,用
        print('********')
        time.sleep(0.1)                # Pause for 1/10 of a second.程序会暂停0.1s

        if indentIncreasing:      #indentIncreasing如果是True代表室递增的
            # Increase the number of spaces:
            indent = indent + 1
            if indent == 20:    #最多有20个空格
                # Change direction:
                indentIncreasing = False
        else:                       #indentIncreasing如果是Flaser代表室递减的
            # Decrease the number of spaces:
            indent = indent - 1
            if indent == 0:       #最少有0个空格
                # Change direction:
                indentIncreasing = True
except KeyboardInterrupt:   #非正常结束
    sys.exit()
    
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值