天天看点

python部分案例代码

# (体重的单位为“千克”,身高的单位为“米”)BMI<18.5为,“偏瘦”
# 18.5<=BMI< 24为正常,24<=BMI< 28位“偏胖”,BMI>=28为,肥胖。?
h = int(input("请输入你的身高,单位为cm:"))
w = float(input("请输入你的体重kg:"))
bmi = w/(h/100)**2
if bmi < 18.5:
    print('偏瘦')
elif 18.5 <= bmi < 24:
    print('正常')
elif 24 <= bmi < 28:
    print('偏胖')
elif bmi >= 28:
    print("肥胖")
else:
    print('请检查一下你是否输入正确!')

# 综合温度和风速的数据判断天气情况
w = float(input('请输入温度:'))
s = float(input("请输入风速:"))

if w > 25:
    if s > 8:
        print('天热,风大')
    else:
        print('天热,风小')
else:
    if s > 8:
        print('天冷,风大')
    else:
        print('天冷,风小')
#
# dict1 = {'姓名':'张三','性别':'女','地址':'毕节'}
#
# for k,v in dict1.items():
#     print('{},{}'.format(k,v))
while True:
    height = float(input('请输入身高,单位为米'))
    if height < 0.3:
        print("你孩子太小,不用收费")
    elif height > 2.5:
        print("你输入错误,这个身高不正常")
    else:
        l_price = 200
        if height <= 1.4:
            print('你门票实际价格{},节约{}'.format(l_price / 2, l_price - l_price / 2))
        else:
            print('你门票实际价格{},节约{}'.format(l_price * 0.9, l_price - l_price * 0.9))
        break
# i = 20
#
# while i > 0 :
#     print(i)
#     i=i-1

# i =100
# sum = 0
# while i >0:
#     sum =sum+i
#     i=i-1
#
# print(sum)


i = 5
sum  = 0
while i <= 100:
    print(i)
    sum =sum+i
    i+=5
    print('循环体内代码')


# print(sum)


# x = y = z =1
# z = 1
# y = 1
#
# x = 1
#
# x,y = y ,x
#
# x = x+y
# 石头剪刀布案例
import random

temp = int(input("请选择你的手势【0】:石头,【1】:剪刀,【2】:布"))
c = random.randint(0,2)
if temp not in [0,1,2]:
    print('你神经病,乱搞')
elif (temp==0 and c==1) or (temp == 1 and c == 2) or (temp ==2 and c==0):
    print('电脑出的手势是',c)
    print('你战胜电脑')
elif (temp==0 and c==0) or (temp == 1 and c == 1) or (temp ==2 and c==2):
    print('电脑出的手势是', c)
    print('平手')
else:
    print('电脑出的手势是', c)
    print('你输了')


# # for嵌套,for和while 都可以嵌套,例如下列案例。
for i in range(1,10):
    for j in range(1,i+1):
        print("{}*{}={}".format(j,i,i*j),end= ' ')
    print('\n')

# i=1
# while i<100:
#     print("*"*i)
#     i+=1
#
# for i in range(1,11):
#     print('*'*i)



# j = 10//2
# for i in range(1,10,2):
#     print(' '*j,end='')
#     j-=1
#     print('*'*i)
#打印等腰三角形图形
j=5
for i in range(1,10,2):
    print(" "*j,end="")
    j-=1
    print("*"*i)


#案例:9*9乘法表打印


for i in range(1,10):
    for j in range(1,i+1):
        print("{}*{}={}".format(j,i,i*j),end=' ')
    print('\n')


for i in range(50):
    print(i)
i=0
while True:
    i+=1
    if i==50:
        continue
    elif i==100:
        break
    print(i)

#案例: 打印1–100数字,但是遇到5,或者是5的倍数就跳过。代码如下:
for i in range(1,101):
    if i==5 or i%5 ==0 or i//10==5:
        continue
    else:
        print(i)
a=0
if a>0:
    print(a)
for i in range(1,10):
    print(i)
    print(a)
#

# 案例判断是否是合法的邮件地址,且提取用户名和服务器地址
temp1 = input("请输入一个邮箱地址")

if temp1.find('@') == -1:
    print("请输入正确的邮箱地址:")
elif temp1.find('@') == 0:
    print("邮箱第一个字符不能为@:")
elif temp1.find('@') == len(temp1) - 1:
    print('@不能结尾,请输入正确邮箱')
else:
    print('用户名为:{},邮箱服务器地址:{}'.format(temp1.split("@")[0], temp1.split("@")[1]))

# 字符串:“”,‘’,'''三引号'''

# str1 = ' [email protected]'
# str2 = "[email protected]"
# str3 = '''[email protected]'''

# temp = str1.find('@')
# print(temp)
# temp1 =str1[::-1]
# print(temp1)
# print(str1.find('@'))
# print(str1.count('n'))
# temp = str1.split('@')
# print(temp[0],temp[1])

#案例:请输入一个邮箱地址:判断邮箱是否合法,提取出其中的用户名和服务器地址
#
temp = input("请输入一个邮箱地址:")
if temp.find("@")==0:
    print('第一个字符不能是@!')
elif temp.find('@')==-1:
    print("你的邮箱地址错误,没有符号@!")
elif temp.find('@')==len(temp)-1:
    print('邮箱最后一个字符不能是@!')
else:
    a = temp.split("@")
    print('你的用户名为:{},你的服务器地址为:{}'.format(a[0],a[1]))

str1 = 'abc'

if 'a' not in str1:
    print('a在str1中')
else:
    print('没在')


#字符串:什么是字符串
#字符串常见的内建函数
# str.format()
# str.find()
# str.index()
# str.count()
# str.split()
# str.capitalize()
# str.replace()
# str.isdigit()
#字符串计算
# + *
# in not

#字符串下标
#字符串
# str1 = 'kingkun@   126.com'
# temp = str1.split('')
# print(temp)
#查询模块
dict2 = {'张三':['男',60,'毕节'],'李四':['男',50,'贵阳']}
for k,v in dict2.items():
    print(k,v)

print('---'*10)
#增加模块
input1 = input('请输入你要添加的姓名')
gender = input("请输入性别")
age = int(input('请输入你的年龄'))
address = input('请输入地址')
temp_list = [gender,age,address]
dict2 = {'张三':['男',60,'毕节'],'李四':['男',50,'贵阳']}
dict2[input1] = temp_list
for k,v in dict2.items():
    print(k,v)

#删除数据模块
dict2 = {'张三':['男',60,'毕节'],'李四':['男',50,'贵阳']}
for k,v in dict2.items():
     print(k,v)
print('-'*14)
del dict2['张三']
for k,v in dict2.items():
     print(k,v)

#修改模块
dict2 = {'张三':['男',60,'毕节'],'李四':['男',50,'贵阳']}
for k,v in dict2.items():
     print(k,v)
print('-'*14)

input1 = input('请输入你要修改的姓名')
gender = input("请输入修改后性别")
age = int(input('请输入修改后你的年龄'))
address = input('请输入修改后地址')
temp_list = [gender,age,address]
dict2[input1] = temp_list
for k,v in dict2.items():
     print(k,v)


# 学生管理系统案例
dict1 = {'姓名':['性别','年龄','地址']}
while True:
    print("*"*10,'学生管理系统',"*"*10)
    temp = int(input("查看学生信息请选择0,添加请选择1,删除请选择2,修改请选择3,退出选择4:"))
    if temp ==0:
        for k,v in dict1.items():
            print(k,v)
    elif temp == 1:
        name = input("请输入学生姓名:")
        gender = input("请输入学生性别:")
        age = input("请输入学生数字年龄:")
        address = input('请输入学生地址:')
        lis_temp = []
        lis_temp.append(gender)
        lis_temp.append(age)
        lis_temp.append(address)
        dict1[name] = lis_temp
        print('你成功添加"{}"信息,所以信息如下:'.format(name))
        for k, v in dict1.items():
            print(k, v)
    elif temp ==2:
        name = input("请输入需要删除学生姓名:")
        del dict1[name]
        print('移除成功')
        for k, v in dict1.items():
            print(k, v)
    elif temp ==3:
        name = input("请输入需要修改学生姓名,如果要修改姓名请删除重新添加:")
        dict1[name][0]=input("请输入修改学生性别:")
        dict1[name][1] = input("请输入修改学生数字年龄:")
        dict1[name][2] = input('请输入修改学生地址:')
        print('修改成功')
        for k, v in dict1.items():
            print(k, v)
    elif temp ==4:
        break
#
#查询
list1 = ['字符串',10,['abc']]
print(list1[2])#使用下标进行查询,下标从0开始
#
# #增加
list1 = ['字符串',10,['abc']]
temp= 'aaaa'
list1.append(temp)#结尾增加元素
print(list1)
#插入
list1 = ['字符串',10,['abc']]
print('原来的字符串',list1)
temp = 'aaaa'
list1.insert(1,temp)#在指定位置删除元素
print('插入后字符串',list1)

#修改
list1 = ['字符串',10,['abc']]
print('原来的字符串',list1)
temp = 'aaaa'
list1[1]=11
print('插入后字符串',list1)

#删除
# list1 = ['字符串',10,['abc']]
# print('原来的字符串',list1)
# list1.remove(10)#移除指定元素
# list1.pop(0)#弹出指定元素,如果不带参数,弹出最后一个
# print('删除后字符串',list1)

#元组
# tuple1 = (1,2,3,4)
# for i in tuple1:
#     print(i)
# #元组(不能修改)但是有嵌套(嵌套内部可以修改)的一定要注意
# tuple1 = (1,2,3,4,[1,2,4])
# print(tuple1)
# tuple1[4][2]=3
# print(tuple1)


# #集合案例  集合是无序的,没有下标,不能用下标索引。
# list1 = [1,2,3,4,5,6,1,2,3,4,5,6,7,]
# set1 = {1,2,3,4,5,6,1,2,3,4,5,6,7}
# tuple = (1,2,3,4,5,6,1,2,3,4,5,6,7,)
#
# temp = set(tuple)
# temp2 = list(set1)
# print(temp)
# print(temp2)
#
# set1.pop()
#
# list1.pop()

# 字典:{'key':'value'}

# 函数:系统内置函数,自定义函数

# print()
# input()

# 案例自定义函数:带参数,不带参数,带返回值,不带返回值

def sum_1():
    a=1
    b=2
    print(a+b)

# 案例带参数,并且默认参数
def sum_2(a=0,b=0):
    print(a+b)

# 案例 带参数:默认,1个参数,多个参数,不定长参数。

def add(a,b):
    print(a+b)

def abc(*args,**kwargs):
    if len(args) > 1:
        print(sum(args))
    else:
        print(kwargs)

# 不定长参数案例

def abc(a,b,*args,**kwargs):#定义可变长函数
    print("参数a={},b={},args={},kwargs={}".format(a,b,args,kwargs))

abc(1,2,3,4,5,6,7,[1,2,3,],(1,2,3),p=0,k=1) #函数调用


#带返回值函数
# 带参数+带返回值
def add(a,b):
    temp = a+b
    return temp

print(add(1,2))

# 无参数,有返回值

def add():
    a=10
    b=20
    return a+b

temp  = add()
print(temp)

#无参数,有返回值
def abc():
    return {"abc":10}

temp = abc()
#
# print(temp)
# 有参数,有返回值
def abc(a,b):
    return {a:b}

temp = abc('king',10)

print(temp)


# 学生管理系统案例(函数版本)
dict1 = {'姓名':['性别','年龄','地址'],'King':['men',10,'毕节']}
temp=0
def suru():
    """这个函数的目的是输入选择数字"""
    print("0:查询,1:增加,2:删除,3:修改,4:退出")
    global temp
    temp = int(input("请输入一个数字"))

def chaxun():
    """查询数据库中的信息"""
    for k,v in dict1.items():
        print(k,v)

def abc():
    '''添加到数据库'''
    xingbie = input('请输入性别')
    nianl = input("请输入年龄")
    dizhi = input("请输入地址")
    dict1[xingming] = [xingbie, nianl, dizhi]


while True:
    suru()
    if temp == 0:
        chaxun()
    elif temp == 1:
        xingming = input("请输入要添加的姓名")
        abc()
        chaxun()
    elif temp == 2:
        xingming = input("请输入要添加的姓名")
        if xingming in dict1.keys():
            del dict1[xingming]
        else:
            print('没有此人,无法删除')
        chaxun()
    elif temp == 3:
        xingming = input("请输入要添加的姓名")
        if xingming in dict1.keys():
            abc()
            chaxun()
        else:
            print('没有此人,无法修改')
    elif temp == 4:
        break
    else:
        print("你输入错误,请重新输入")