#位置实参,关键字是参>>>默认值,,注意顺序,把已经有默认值的放在最后
#*topping这个可以接收任何实参,一般放形参最后,它是个空元组,当作列表一样就行
def pet(name,size='27'):#默认值
print(name+' '+size)
n=input('please get me a name:')
s=input('give ma a size:')
pet(size='25',name='dog')#关键字
pet(n,s)
pet(n,s)
#【dog 25
# dog 30
# dog 27】
#主要是这里用默认值把middle变成可选的,且用if来应变多种情况
def get_name(first,last,middle=' '):
if middle:
full_name=first+''+middle+''+last
else:
full_name=first+''+last
print(full_name)
F=input('please get me a first name:')
L=input('please get me a last name:')
M=input('please get a middle name:')
get_name(F,L,M)
#返回字典,只需用一个返回return,把字典返回出来就行
def zidian(k,v):
dictionary[k]='v'
return dictionary
dictionary={}#创建空字典
key=input('please get me k:')
value=input('please get me y:')
a=zidian(key,value)#函数把字典放回给了a
print(a)
#有用的就是尽量让user想退出时退出
while True:
print("when you enter 'q',it will end")
name=input('get me you name :') #先输再判断
if name=='q':
print('end')
break
last=input('get me a last :')
if last=='q':
print('end')
break
print('\n'+name+' '+last+'\n')
#传递列表,列表作参数可以直接传递
def greet(names):
for name in names:
print('hello,'+name)
users=['libai','dufu','wo']
greet(users)
#用切片[:]创建副本,输出两个列表时,中间不用连接号即+‘’+
names=[]
name=['libai','dufu','wo']
def carry(n):
for c in n:
names.append(c)
carry(name[:])
print(names,name)
'''导入模块【文件】有3种第一种是设 文件.py 1 和 99
然后【直接用函数名调用】,注意文件1和99必须在同一个目录,且都是.py文件
第一:全部导入:impot 99 , 在文件 1 中调用其中函数是格式: 99.+函数名(参数),注意有没有return,或print
第二:导入特定函数:from 99 import 加函数名1,函数名2,函数名3
第三:导入所有函数:from 99 import*
as可以给模块命名:impor 99 as p,调用是用p就行'''