1.乘法口诀表
for i in range(1,10):
for j in range (1,i + 1):
ji = i * j
print("{}*{}={}".format(i,j,ji),end='\t')
print()
2.11点游戏
'''
11点游戏
大小王用14 15 表示
自动生成牌 J-大王 0.5分 1-10按照牌面值
3个玩家
发牌
'''
import random
wang_list = ['("小王",14)','("大王"),15']
color_list = ["红桃",'黑桃','梅花','方片']
num_list = range(1,14)
poke_list = [(color,num) for color in color_list for num in range(1,14)]
poke_list.extend(wang_list)
result = {}
# #洗牌 打乱list顺序
# random.shuffle(poke_list)
user_list = ['Jack','Jim','Lucy']
for user in user_list:
score = 0
idx = random.randint(0,len(poke_list) - 1)
poker = poke_list.pop(idx)
_,num = poker
if num > 10:
score += 0.5
else:
score += num
while True:
choice = input("if go on ?(y/n):")
choice = choice.upper()
if choice not in {"Y","N"}:
print("input error please try again")
continue
if choice == "N":
result[user] = score
break
else:
idx = random.randint(0, len(poke_list) - 1)
poker = poke_list.pop(idx)
_, num = poker
if num > 10:
score += 0.5
else:
score += num
if score <= 11:
continue
score = 0
result[user] = score
print("爆了")
break
print(result)
"""
根据要求实现资源下载器
1.启动后 让用户选择专区 每个专区用单独的函数实现,提供的专区如下:
下载 花瓣网图片专区
抖音短视频专区
NBA锦集专区
2.在用户选择某个功能后 表示进入某个下载专区 在里面循环提示user 可以下载内容的选项 用户只要选择序号就可以下载指定的资源
3.在某个专区中,如果用户输入 Q/q 表示退出上一级,回到选择区
4.在选择区中输入Q/q 则退出整个程序
每个专区实现下载案例如下:
"""
import requests
def run():
"""启动程序"""
func_dic = {
"1":image,
"2": nba,
"3": video,
}
print("1.图片专区","2.NBA专区","3.短视频专区" )
while True:
choice = input("请选择:")
func = func_dic.get(choice)
print(func)
if not func:
continue
func()
def image():
"""图片专区功能"""
image_dic = {
"1":("image1","https://huaban.com/pins/5042741077"),
"2":("image2","https://huaban.com/pins/5077852312")
}
for k,v in image_dic.items():
msg = "{}.{}".format(k,v[0])
print(msg)
choice = input("please choice:")
title, url = image_dic[choice]
file_name = "{}.jpg".format(title)
_download(file_name,url)
def nba():
"""NBA function"""
nba_dic = {
"1":{"title":"闪电新闻推出解码“中国式现代化”系列短视频","url":"https://haokan.baidu.com/v?pd=wisenatural&vid=6566621781949319567"},
"2":{"title":"钟表、花瓶 3D打印实现废物利用", "url": "https://haokan.baidu.com/v?vid=3071146304186853769&&"
}
}
_video_nba(nba_dic)
# for v, k in nba_dic.items():
# msg = "{}.{}".format(v,k['title'])
# print(msg)
#
# choice = input("please choice:")
# info_dic = nba_dic[choice]
# name = info_dic['title']
# url = info_dic['url']
# filename = "{}.mp4".format(name)
# _download(filename, url)
def video():
"""Video function"""
video_dic = {
"1":{"title":"阿龙过来给尔普修水池,真诚朴实善良,邀请我到她家过年还相亲","url":"https://www.douyin.com/video/7164291628558077188"},
"2":{"title":"曾经的我们也被人羡慕", "url": "https://www.douyin.com/video/7164357861185801480"
}
}
_video_nba(video_dic)
# for v, k in video_dic.items():
# msg = "{}.{}".format(v,k['title'])
# print(msg)
#
# choice = input("please choice:")
# info_dic = video_dic[choice]
# name = info_dic['title']
# url = info_dic['url']
# filename = "{}.mp4".format(name)
# _download(filename, url)
def _download(filename,url):
res = requests.get(url=url,headers={
"user-agent":"Mozilla/5.0 (Windows NT 10.0;Win64; x64) AppleWebKit/537.36(KHTML, likeGecko) Chrome/103.0.0.0Safari/537.36" })
print(res)
with open(filename,mode='wb') as f:
f.write(res.content)
def _video_nba(data_dict):
for v, k in data_dict.items():
msg = "{}.{}".format(v, k['title'])
print(msg)
choice = input("please choice:")
info_dic = data_dict[choice]
name = info_dic['title']
url = info_dic['url']
filename = "{}.mp4".format(name)
_download(filename, url)
3.装饰器练习
"""计算一个函数的执行时间"""
import time
def coast(arg):
def inner():
start = time.time()
res = arg()
end = time.time()
print(end - start)
return res
return inner
@coast
def func():
data = 0
for i in range(10000000000):
data += i
return data
func()
"""
在函数执行之前输出一个befroe
实现对原函数的返回值 + 100
"""
def before(arg):
def inner():
print("befroe")
return arg()
return inner
def update_return_value(arg):
def inner():
res = arg()
res = res + 100
return res
return inner
@update_return_value
@before
def f1():
return 123
t = f1()
print(t)
def repeat_func(arg):
def inner():
result = 0
for i in range(5):
res = arg()
result += res
return result
return inner
@repeat_func
def f1():
return 123
t = f1()
print(t)
关于装饰器的参数
def outer(args):
def inner(a1,a2):
return args(a1,a2)
return inner
@outer
def plus(v1,v2):
return v1+v2
res = plus(1,3)
print(res)
def outer(args):
def inner(a1):
return args(a1)
return inner
@outer
def plus(v1):
return v1
res = plus(1)
print(res)
def outer(func):
def inner(*args,**kwargs):
return func(*args,**kwargs)
return inner
@outer
def plus(v1):
return v1
res = plus(1)
print(res)
"""
带参数的装饰器
"""
def tt(count):
def outer(func):
def inner(*args,**kwargs):
r = 0
for i in range(count):
res = func(*args,**kwargs)
r += res
return r
return inner
return outer
@tt(5)
def f(x):
return x
res = f(100)
print(res)
标签:脚本,return,python,res,dic,print,choice,def
From: https://www.cnblogs.com/W505020235/p/16876663.html