3.函数
测试程序 test_301.py
def hello():
print ('Howdy!')
print ('howdy!!!')
print ('hello there.')
hello()
hello()
hello()
1)def 语句和参数
测试程序 test_302.py
def hello(name):
print ('hello, ' + name)
hello('Alice')
hello('Bob')
2)返回值和return语句
测试程序 test_303.py
import random
def getAnswer(answerNumber):
if answerNumber == 1:
return 'It is certain'
elif answerNumber == 2:
return 'It is decidedly so'
elif answerNumber == 3:
return 'Yes'
elif answerNumber == 4:
return 'Reply hazy try again'
elif answerNumber == 5:
return 'Ask again later'
elif answerNumber == 6:
return 'Concentrate and ask again'
elif answerNumber == 7:
return 'My reply is no'
elif answerNumber == 8:
return 'Outlook not so good'
elif answerNumber == 9:
return 'Very doubtful'
#r = random.randint(1,9)
#fortune = getAnswer(r)
#print(str(r) +':::' + fortune )
print(getAnswer(random.randint(1,9)))
print("hello",end=' ')
print("World")
print('cats','dogs','mice')
print('cats','dogs','mice',sep=',')
3)None值
4)关键字参数
顺序参数和关键字参数。关键字参数,用于“可选变元“。
5)调用栈
函数调用过程是层层嵌套的。
测试程序test_304.py
def a():
print('a() starts')
b()
c()
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()
6)局部和全局作用域
变量和变元所作用的区域。
函数内定义的在局部作用域,称为局部变量。
处于全局定义的称为全局变量。
局部变量不能在全局作用域中使用,也不能在其它局部作用域中使用。
全局变量可以在局部作用域中使用。
名称相同的局部变量和全局变量
测试程序 test_305.py
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)
7)global语句
在函数内要使用全局变量,要用global语句进行申明。
测试程序 test_306.py
def spam():
global eggs
eggs = 'spam' # this is the global
def bacon():
eggs = 'bacon' # this is a local
def ham():
print(eggs) # this is a global
eggs = 42
print('fore:' + str(eggs))
spam()
print('end:' + eggs)
8)异常处理
try …… except
测试程序 test_307.py
def spam(divideBy):
# try:
return 42 / divideBy
# except ZeroDivisionError:
# print('Error : Invalid argument.')
try:
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
except ZeroDivisionError:
print('Error : Invalid argument.')
9)小程序Zigzag
测试程序 test_308.py
import time,sys
indent =0 #how many spaces to indent.
indentIncreasing = True #Whether the indentation is increasing or not.
try:
while True: # The main program loop.
print(' ' * indent, end = '')
print('********')
time.sleep(0.1) #Pause for 1/10 of a second.
if indentIncreasing:
# Increase the number of spaces:
indent = indent + 1
if indent == 20:
#Change direction:
indentIncreasing = False
else:
# Decrease the number of spaces:
indent = indent - 1
if indent == 0:
#Change direction:
indentIncreasing = True
except KeyboardInterrupt:
sys.exit()
10)作业collatz
测试程序 test_309.py
import sys
def collatz(numb):
if numb % 2 == 0:
return numb // 2
else :
return 3 * numb + 1
print('Enter number:' , end='')
numst = input()
try:
num = int(numst)
except ValueError:
print('please input int argument.')
sys.exit()
while num != 1:
num = collatz(num)
print(num)