栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python(路飞学城听课整理)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python(路飞学城听课整理)

多分支

score int(input( Score: ))
if score 89:
 print( A )
elif score 79:
 print( B )
elif score 59:
 print( C )
elif score 39:
 print( D )
else:
 print( E )

循环 猜年龄

需求 最多允许猜三次 猜对了就退出程序

real_age 21
for i in range(3):
 guess_age int(input( guess a number: ))
 if guess_age real_age:
 print( 大了大了 )
 elif guess_age real_age:
 print( 小了小了 )
 else:
 exit( bingo )

black_gf_age 25
count 0
while True:
 if count 3:
 count 1
 guess int(input( 猜猜⿊姑娘多⼤了 : ))
 if guess black_gf_age:
 print( 猜的太⼤了 往⼩⾥试试... )
 elif guess black_gf_age:
 print( 猜的太⼩了 往⼤⾥试试... )
 else:
 exit( 恭喜你 猜对了... ) # 退出程序
 else:
 choice input( 猜了3次还不对 真是笨呀 还玩么? [y/Y or n/N] ).strip()
 if len(choice) 0:
 continue # 不能写空值
 if choice in ( y , Y ):
 count 0
 elif choice in ( n , N ):
 exit( bye. )
 else:
 print( 请输⼊正确的选项... )

打印房间号

⼀栋楼有3层 每层6间屋⼦ 要求你把本楼所有的房间号打印⼀遍 格式“1层-104” “2层-205“

for i in range(1,4):
 print(f -----------{i}层----------- )
 for j in range(1,7):
 print(f {i}层-{i}0{j}室 , end )
 print()

在Python中 print()函数默认是换行的。让print()函数不换行 只要指定print()函数的end参数为空就可以了。 默认是’n’

打印三角形
for i in range(1,10):
 if i 5:
 print( * *i)
 else:
 print((10-i)* * )

打印九九乘法表
for i in range(1, 10):
 for j in range(1, i 1):
 print(f {i}x{j} {i * j} ,end )
 print()

京牌摇号

需求

允许⽤户最多选3次每次放出5个车牌供⽤户选择京[A-Z]-[xxxxx], 可以是数字和字母在组合

random模块

import random # 导⼊random模块
random.choice( abcdefghi ) # 从中随机选择一个字母 参数也可以是⼀个列表
s abcdefghijklmn 
random.sample(s, 3) # 从数据源s中随机取出3个值
random.randint(1, 100) # 在1~99中选一个数字

string模块

import string # 导⼊string模块
string.ascii_letters
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 
string.ascii_uppercase
# ABCDEFGHIJKLMNOPQRSTUVWXYZ # ⼤写字⺟
string.ascii_lowercase # ⼩写字⺟
# abcdefghijklmnopqrstuvwxyz 
string.punctuation # 打印特殊字符
# ! #$%  ()* ,-./:; ? [\]^_ {|}~ 
string.digits # 打印数字
# 0123456789 

连接

 .join([ a , c , S ])
# acS

代码实现

import random
import string
car_num_sample string.digits string.ascii_uppercase
count 3
while count 0:
 count - 1
 num_list []
 for i in range(1,6):
 n1 random.choice(string.ascii_uppercase)
 n2 .join(random.sample(car_num_sample, 5))
 car_num f 京{n1}-{n2} 
 num_list.append(car_num)
 print(i, car_num)
 choice input( choice: ).strip()
 if choice in num_list:
 exit(f 恭喜你选购成功 您的新⻋牌是{choice} )
 else:
 print(f 未选中 还有{count}次机会 )

年会抽奖程序

张三科技有限公司有300员⼯ 开年会抽奖 奖项如下
⼀等奖 3名 泰国5⽇游
⼆等奖6名 Iphone⼿机
三等奖30名 避孕套⼀盒

规则

共抽3次 第⼀次抽3等奖 第2次抽2等奖 第3次压轴抽1等奖每个员⼯限中奖⼀次 不能重复

解题思路

⽣成⼀个员⼯列表 ⽤random模块从⾥⾯取随机值取完值之后 ⽴刻从员⼯⼤列表⾥把中奖⼈删掉 即可防⽌其再次中奖
import random
import time
staff_number []
for i in range(0, 300):
 staff_number.append(f 员工{i} )
level [30, 6, 3]
for i in range(3):
 winpeople random.sample(staff_number, level[i])
 for p in winpeople:
 staff_number.remove(p)
 print(f 抽中{3 - i}等奖的人是: ,winpeople)
 time.sleep(1)

字符串 常用操作
a hello world 
print(a.center(50, - ))
print(a.count( l ))
print(a.count( o , 0, 5))
print(a.endswith( ld )) # 判断结尾
print(a.startswith( he )) # 判断开头
print(a.find( l )) # 字符查找 如果没找到 返回-1 否则 返回所查字符的索引
print(a.isdigit())
print( 22 .isdigit()) # 判断是否为整数
list [ alex , hello , world ]
print( - .join(list)) # 拼接字符串
print(a.replace( l , M , 1)) # 字符串替换
print(a.split( l )) # 分隔符
print(a.strip()) # 移除字符串头尾指定的字符
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/266834.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号