首页 > 其他分享 >函数

函数

时间:2024-01-17 19:25:02浏览次数:23  
标签:name a1 user print password def 函数

语句定义格式:

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

相关文章

  • 函数作用域(二)
    '''作用域:变量的作用范围全局变量:定义在函数外部的变量局部变量:定义在函数内部的变量函数内部可以访问全局变量,但是不能直接修改如果要修改全局变量,需要借助关键字global声明函数内部用global修改了全局变量那么全局变量已经被修改了'''n=100deffunc():globaln#声明全......
  • 【学习笔记】数论函数与莫比乌斯反演
    一.数论函数基础数论函数:满足值域为整数的函数。本文下述的数若无特殊说明均为整数。若无特殊说明则钦定\(\displaystylen=\prod_{i=1}^kp_i^{e_i},p_i\in\mathbb{P}\)。\(\mathbb{P}\)表示质数集合,\(p_i\)互不相同。介绍几个常见的数论函数:\(I(n)\):恒等函数,无论\(n\)......
  • 无涯教程-SQL Numeric Functions函数
    SQL数字函数主要用于数字操作和/或数学计算。下表详细介绍了数字函数-Sr.No.Function&描述1ABS()返回数值表达式的绝对值。2ACOS()返回数值表达式的反余弦值。如果该值不在-1到1之间,则返回NULL。3ASIN()返回数字表达式的反正弦值。如果值不在-1到1的范围内,则返......
  • C++中setw和setfill函数的结合应用
    一、头文件头文件为#include<iomanip>其中io代表输入输出,manip是manipulator(操纵器)的缩写iomanip的作用:主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,setbase,setprecision等等。它是I/O流控制头文件,就像C里面的格式化输出一样。二、setw函数s......
  • 无涯教程-SQL RAND Function函数
    SQL具有RAND函数,可以调用该函数以生成介于0和1之间的随机数-SQL>SELECTRAND(),RAND(),RAND();+------------------+-----------------+------------------+|RAND()|RAND()|RAND()|+------------------+-----------------+-----......
  • 无涯教程-SQL SUM Function函数
    SQLSUM函数用于查找各种记录中一个字段的总和。要了解SUM函数,请考虑一个employee_tbl表,该表具有以下记录-SQL>SELECT*FROMemployee_tbl;+------+------+------------+--------------------+|id|name|work_date|daily_typing_pages|+------+------+---......
  • 图解函数脚本处理传入参数
    1.函数脚本传入参数:1.1.先修改函数脚本文件: 1.2.开发一个新脚本,执行该函数: 1.3.执行该脚本func3.sh:2.图解shell脚本处理参数: ......
  • 无涯教程-SQL - ADDDATE()函数
    下表列出了可通过SQL使用的所有重要的与日期和时间相关的重要功能。RDBMS还支持其他各种功能。给定的列表基于MySQLRDBMS。Sr.No.Function&Description1ADDDATE()添加日期2ADDTIME()增加时间3CONVERT_TZ()从一个时区转换到另一个时区4CURDATE()返回当前日......
  • 无涯教程-SQL COUNT Function函数
    SQLCOUNT函数是最简单的函数,在计算预计由SELECT语句返回的记录数时非常有用。要了解COUNT函数,请考虑一个employee_tbl表,该表具有以下记录-SQL>SELECT*FROMemployee_tbl;+------+------+------------+--------------------+|id|name|work_date|daily_typ......
  • 详解shell环境与函数执行
    理清楚函数在shell环境中加载场景:函数定义和执行,分开在不同的文件中,Linux自带的诸多脚本,就是基于该形式使用的1.函数写在一个文件中,只定义,不执行2.另外一个脚本,读取该函数文件,且加载该函数第一步:先生成一个脚本my_func.sh,作用只记录函数,不运行第二步:如......