'''
格式:
def 函数名([参数,...]):
代码
函数名:
get_name()
search()
代码:
封装重复内容
'''
import random
def generate_code():
# 生成验证码
s = 'sdhsyfgifihisjvojvod'
code = ''
for i in range(4):
r = random.choice(s)
code += r
print(code)
# 调用函数
# generate_code()
'''
输入用户名和密码,验证是否正确
'''
def login():
username = input('用户名:')
password = input('密码:')
if username == 'admin' and password == '1234':
print('登录成功')
else:
print('登录失败')
# login()
'''
参数:
1.无参数
2.有参数
无参数:
def 函数名():
pass
有参数:
def 函数名(参数1,参数2,参数3,...):
pass
'''
import random
def generate_code(n):
# 生成验证码
s = 'sdhsyfgifihisjvojvod'
code = ''
for i in range(n):
r = random.choice(s)
code += r
print(code)
# generate_code(4)
'''
带多个参数的函数
'''
def get_sum(a, b):
# if isinstance(a, int) and isinstance(b, int):
if type(a) == type(b):
s = a + b
print(s)
else:
# print('类型错误')
print('类型不一致')
# get_sum(2, 3)
# get_sum('hello', 'world')
# get_sum('20', 5)
def islogin(username, password, isremmeber = False):
print('是否记住密码?', isremmeber)
if username == 'admin' and password == '1234':
print('登录成功')
else:
print('登录失败')
# islogin('admin', '1233')
'''
默认值参数:在定义函数的时候,有一个或者多个参数已经赋好值
def 函数名(参数1,参数2,参数3 = 值,参数4 = 值,参数5 = 值):
pass
调用特点:
函数名(值1,值2)
注意:
1.在定义函数的时候,普通参数要位于默认值参数的前面
2.默认参数的顺序是固定的
解决方式:
调用时使用关键字参数赋值
'''
def borrow_book(bookname, number = 1):
print('进入借书系统...')
print('要借阅的书名是:{},借阅的数量是:{}'.format(bookname, number))
# borrow_book('狂人日记')
# borrow_book('草房子', 3)
library = ['python精通', 'mysql', '数据分析', '人工智能']
def add_book(bookname):
library.append(bookname)
print('图书添加成功')
def show_book(books):
for book in books:
print(book)
# add_book('新概念英语')
# show_book(library)
list1 = [23, 45, 77, 80, 59, 10]
def get_list(list_1):
# new_list = []
# for e in list_1:
# if e >= 50:
# new_list.append(e)
new_list = [e for e in list_1 if e >= 50]
print(new_list)
# get_list(list1)
def remove_from_list(list_1):
n = 0;
while n < len(list_1):
if list_1[n] < 50:
list_1.remove(list_1[n])
else:
n += 1
print(list_1)
# remove_from_list(list1)
'''
可变参数:
*args
**kwargs
拆包和装包:
函数装包:
def 函数(*args):---->此时会出现装包操作
pass
函数(1,2,3,4)
拆包:
list,tuple,set
调用的时候:
函数(*list) | 函数(*tuple) | 函数(*set)
拆包过程
'''
#
def get_sum(*a):
print(a)
s = 0
for i in a:
s += i
print('和:', s)
get_sum(1, 2)
get_sum(1, 2, 3, 4, 5)
ran_list = [23, 45, 12, 44, 78, 39, 29]
get_sum(*ran_list)
# a, *b, c = 1, 2, 3, 4, 5
# print(a)
# print(b)
# print(c)
'''
可变参数:**kwargs
关键字参数
在函数调用的时候必须传递关键字参数,才可以将其转换成key:value
装到字典中
'''
def show_book(**kwargs):
print(kwargs)
for k, v in kwargs.items():
print(k, v)
# show_book()
# show_book(bookname = '西游记', author = '吴承恩', number = 5)
#
# book = {'bookname': '红楼梦', 'author': '曹雪芹', 'number': 5}
# show_book(**book)
'''
可变参数:
*args **kwargs
arguments
'''
def show_book(*args, **kwargs):
print(args)
print(kwargs)
# show_book()
book = {'bookname': '坏小孩', 'author': 'zzz', 'number': 5}
show_book('龙少', '消防', **book)
# print(book, 'hello', sep='--')
# print('{}{}{}'.format('AA', 'BB', 'CC'))
# print('{name}{age}{sex}'.format(name = 'zhangsan', age = 19, sex = '男'))
#
# result = "-".join(["a", "b"])
# print(result)
'''
返回值
'''
def get_sum(*args):
total = 0
for i in args:
total += i
return total
t = get_sum(1, 2, 3)
x = 100
x = x + t
# print(x)
def get_maxandmin(numbers):
for i in range(0, len(numbers)-1):
for j in range(0, len(numbers)-1-i):
if numbers[j] > numbers[j+1]:
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
min = numbers[0]
max = numbers[-1]
return min,max
list1 = [34, 11, 78, 90, 100, 23, 56, 88, 91]
result = get_maxandmin(list1)
# print(result)
'''
全局变量和局部变量
global关键字的添加:
只有不可变的类型才需要添加global
可变的类型不需要添加global
'''
a = 100
def test1():
# 局部变量
a = 0
b = 8
print('a =', a)
print('b = ', b)
def test2():
# 只有用global声明了,才是全局变量
global a
a = 99
print('a = ', a)
# test1()
# test2()
# print(a)
'''
可变与不可变
不可变:当改变变量的值时,地址发生了改变
类型:int str float bool tuple
可变:里面的内容发生了改变,但是地址没有发生改变
类型:list dict set
'''
# a = 100
# print(id(a))
# a = 90
# print(id(a))
#
# s = 'abc'
# print(id(s))
# s = 'xyz'
# print(id(s))
# t1 = (1, 2, 3)
# print(id(t1))
# t1 = (1, 2, 3, 4)
# print(id(t1))
# 列表
# l1 = [1, 2, 3]
# print(id(l1))
# l1.append(4)
# print(id(l1))
'''
练习:停车收费
'''
# 没有车辆
car_park = []
def enter():
print('欢迎进入xxx停车场')
number = input('输入车牌号码:')
# 构建结构{’车牌‘:[0,5]}
car = {}
car[number] = [0]
# 添加到car_park
car_park.append(car)
print('{}已进场'.format(number))
def go_out():
number = input('输入车牌号码:')
# 判断汽车是否进场
for car in car_park:
if number in car:
# 记录结束时间
time = random.randint(0, 24)
time_record = car.get(number)
time_record.append(time)
# 计算花费
total = time*4
print('{}停车{}小时,应缴费{}元'.format(number, time, total))
break
else:
print('此车未进场')
enter()
go_out()