一、def语句和参数
#!/usr/bin/env python #coding:utf-8 def hello(name): print('Hello ' +name) hello('dingkai') hello('lianghui')
1.变元:以上代码中,'name'是变元(“变元”是一个 变量);
(1)保存在变元中的值,在函数返回后就丢失
二、返回值和retrun语句
1返回值:函数调用求值的结果,称为函数的“返回值”。
2.return
(1)用 def 语句创建函数时,可以用 return 语句指定应该返回什么值。return 语句包
(2)含以下部分:
return 关键字;
函数应该返回的值或表达式。
return 关键字;
函数应该返回的值或表达式。
#!/usr/bin/env python3 #coding:utf-8 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(fortune) print(getAnswer(random.randint(1,9)))