语句定义格式:
def 函数名(..):
代码块
调用函数
函数名(..)
函数名(..)
定义
def print_info():
print('dyj大sb')
print(666)
a1=10
b1=11
c1=a1+b1
print(c1)
调用
print_info()
import fileinput
import time
编写函数的注意事项
- 函数的名字,尽量使用小写英文单词,使用下划线拼接的方式 printInfo()
- 函数不调用不执行
函数使用的好处
- 降低代码的冗余度
- 提高程序编写的灵活性
位置传参
- 形式参数:指的是函数定义时上面的参数
- 实际参数:调用时实际传入的值
def fun1(a1,b1,c1):
d1 = a1+b1+c1
print(d1)
fun1(11,22,33)
关键字传参
def fun1(a1,b1,c1):
d1 = a1+b1+c1
print(d1)
fun1(c1=11,a1=22,b1=33)
混合传参
def fun1(a1,b1,c1):
d1 = a1+b1+c1
print(d1)
fun1(11,22,c1=99)
注意:混合传参时,关键字传参必须是最后一个传入****
def get_str_count(big_str,s):
count=0
for i in big_str:
if i==s:
count=count+1
print(f'{s}字符在字符串中出现了{count}次')
get_str_count('kasiacauidhsaujfhnaiujhcs','a')
def sum1(a1,b1):
if type(a1)int and type(b1)int:
print(f'{a1}+{b1}={a1+b1}')
sum1(19,12)
def sum1(a1,b1,c1):
if type(a1)int and type(b1)int and type(c1)==int:
print(f'{a1}+{b1}+{c1}={a1+b1+c1}')
sum1(19,12,c1=22)
def sum1(*a1):
sum_number=0
for i in a1:
if type(i)==int:
sum_number+=i
print(f'总和为{sum_number}')
sum1(13,21,45,67,55)
def fun1(**a2):
a2={'key':'value'}
print(fun1)
fun1(a8=10)
def sum1(a1,b1):
c1=a1+b1
return c1
res1=sum1(11,22)
print(res1)
def sum1(*args):
res=0
for i in args[0]:
if type(i)int:
res+=i
return res
def get_list():
list1=[]
while True:
number=input('请输入要相加的整数:')
if number.upper()'Q':
break
if number.isdecimal():
list1.append(int(number))
return list1
list2=get_list()
res1=sum(list2)
print(f'{res1}')
import os
def fun1(path,key_word):
list1=[]
if not os.path.exists(path):
return list1
with open(path,mode='r',encoding='UTF-8') as f:
for line in f:
line=line.strip()
if key_word in line:
list1.append(line)
return list1
list2=fun1('D:\projects\PyCharmProjects\d5\sgyy.txt',';刘备')
print(list2)
for i in list2:
print(i)
def fun1():
pass
def fun2():
print(11)
print(22)
def fun3():
print(32)
print(21)
fun3()
定义3个函数,分别代表注册,登录,查看用户信息,一开始,提供一个列表给用户选择(1. 注册 2. 登录 3. 查看用户信息)
import time
def list1():
print('欢迎登录系统'.center(50,'~'))
print('1.注册\n''2.登录\n''3.查看用户信息\n')
list1()
def register():
global user_name
global user_password
global writelines
print('欢迎注册')
user_name=input('请输入用户名:')
user_password=input('请输入用户密码:')
with open('tset2.txt','a',encoding='UTF-8') as f:
f.writelines(f'{user_name},{user_password}\n')
time.sleep(3)
print('注册成功')
def login():
print('欢迎登录')
user_name=input('请输入用户名:')
user_password = input('请输入密码')
with open('tset2.txt','r',encoding='UTF-8') as f:
text=f.read()
line1=(text.split('\n'))
for i in writelines:
if list[0]user_name
print(inputt('请输入密码:'))
if list[1]user_password
print('登录成功')
else:
print('没有该用户,请前往注册')
def login():
print('欢迎登录')
user_name = input('请输入用户名:')
user_password = input('请输入密码:')
with open('tset2.txt', 'r', encoding='UTF-8') as f:
text = f.read()
line1 = text.split('\n')
if user_name in line1 and user_password == line1.index(user_password):
print('登录成功')
else:
print('没有该用户,请前往注册')
login()
def show():
print('用户信息')
with open('tset1.txt','r') as f:
print(f'用户密码是{user_password}',user_password)
if name == 'main':
# 列出菜单
list1=()
dict1 = {
'1': register,
'2': login,
'3': show
}
flag = True
while flag:
choice = input('请输入您的选择(1/2/3): ')
if choice not in dict1:
print('没有您所选的选项!重新输入')
continue
fun1 = dict1.get(choice)
import time
def list1():
print('欢迎登录系统'.center(50,'~'))
print('1.注册\n''2.登录\n''3.查看用户信息\n')
def register():
global user_name
global user_password
global writelines
print('欢迎注册')
user_name=input('请输入用户名:')
user_password=input('请输入用户密码:')
with open('tset2.txt','a',encoding='UTF-8') as f:
ssa1=f.write(f'{user_name},{user_password}\n')
time.sleep(3)
print('注册成功')
print(type(ssa1))
def login():
print('欢迎登录')
user_name=input('请输入用户名:')
user_password = input('请输入密码:')
with open('tset2.txt','r',encoding='UTF-8') as f:
text=f.readlines()
index=lines.index('use_password')
line1=(text.split('\n'))
for i in line1:
if tset1.txt[0]user_name and tset1.txt[1]user_password:
print('登录成功')
else:
print('没有该用户,请前往注册')
def login():
print('欢迎登录')
user_name=input('请输入用户名:')
user_password = input('请输入密码:')
with open('tset2.txt','r',encoding='UTF-8') as f:
text=f.read()
line1=(text.split('\n'))
for i in line1:
if i[0]user_name and i[1]user_password:
print('登录成功')
break
else:
print('没有该用户,请前往注册')
def show():
print('用户信息')
with open('tset1.txt','r') as f:
print(f'您密码是{f.user_password.strip()}',f.user_password.strip())
def show():
with open('tset2.txt', 'r') as file:
user_names = file.read().splitlines()
passwords = file.read().splitlines()
for i, (user_name,user__password) in enumerate(zip(user_names, user_password)):
print(f'User name: {user_name}, Password: {user_password}, Index: {i+1}')
def show():
with open('tset2.txt', 'r') as file:
user_names = file.read().splitlines()
passwords = file.read().splitlines()
for i, (user_name, user_password) in enumerate(zip(user_names,user_names)):
print(f'User name: {user_name}, Password: {user_password}')
if name == 'main':
# 列出菜单
list1=()
dict1 = {
'1': register,
'2': login,
'3': show
}
flag = True
while flag:
choice = input('请输入您的选择(1/2/3): ')
if choice not in dict1:
print('没有您所选的选项!重新输入')
continue
fun1 = dict1.get(choice)
fun1()
标签:name,a1,user,print,password,def,函数
From: https://www.cnblogs.com/pecular/p/17970840