案例一
"""
优惠券
"""
import random
import string
phones = ['一加', '小米', '华为']
coupons = {''.join(random.choices(string.printable[:62], k=9)): 1
for _ in range(100)}
print("优惠券列表 : {0}".format(coupons))
print({'coupon', 0})
endFlag = 100
targetCoupons = {}
while endFlag > 0:
coupon = input("请输入优惠券:")
if coupons.get(coupon):
if targetCoupons.get(coupon) is None:
new_coupon = {coupon: 0}
targetCoupons.update(new_coupon)
print("领取成功,赠送您<<{0}>>手机一部!".format(random.choice(phones)))
endFlag -= 1
else:
print("您已经领取了优惠券")
else:
print("优惠券不存在!")
print("优惠券发放结束")
案例二
"""
优惠券
"""
import random
import string
phones = ['一加', '小米', '华为']
coupons = {''.join(random.choices(string.printable[:62], k=9)): 1
for _ in range(2)}
print("优惠券列表 : {0}".format(coupons))
targetCoupons = {}
while len(coupons) != len(targetCoupons):
coupon = input("请输入优惠券:")
if coupons.get(coupon):
if targetCoupons.get(coupon) is None:
new_coupon = {coupon: 0}
targetCoupons.update(new_coupon)
print("领取成功,赠送您<<{0}>>手机一部!".format(random.choice(phones)))
else:
print("您已经领取了优惠券")
else:
print("优惠券不存在!")
print("优惠券发放结束")
案例三
"""
缓存数据 - 优惠券
"""
import json
import random
import string
phones = ['一加', '小米', '华为', '华为']
nums = [1, 1, 2, 2, 3, 4, 5]
print(set(nums))
coupons = {''.join(random.choices(string.printable[:62], k=9)): 1
for _ in range(50)}
print("优惠券列表 : {0}".format(coupons))
print("发奖品列表 : {0}".format(phones))
with open("./coupon.json", mode='w') as f:
json.dump(coupons, f)
import json
import random
"""
加载数据 - 优惠券
"""
phones = ['一加', '小米', '华为']
print("===优惠券活动开始!=== \n 本期奖品如下 {0}".format(phones))
with open("./coupon.json", mode='r') as f:
coupons = json.load(f)
if coupons is None:
print("优惠券为空")
else:
for index in coupons.keys():
key = input("请输入优惠券:")
if key in coupons.keys():
if coupons[key] == 1:
print("恭喜您领取成功,赠送您<<{0}>>手机一部!".format(random.choice(phones)))
coupons[key] = 0
else:
print("您已经领取了优惠券")
else:
print("优惠券不存在!")
end = input("输入<< end >>停止活动:")
if "end" == end:
break
print("\n ===优惠券活动已结束!===")
with open("./coupon.json", mode='w') as f:
json.dump(coupons, f)
```
```
````
{
"w3oCkrYKv": 1,
"KuZllOikb": 1,
"YA20XLV47": 1,
"xhiYVatij": 1,
"EdizfYKOe": 1,
"jIgPXSkaz": 1,
"VWi359Lhm": 1,
"mR9aWlwTd": 1,
"H60fpZQeQ": 1,
"TtoFRcWq9": 1,
"Lk8DrVkeq": 1,
"b7qLSoXZL": 1,
"BcqVm4VGR": 1,
"FnVaEDVhG": 1,
"JCHx74I7G": 1,
"VQTHaxeyd": 1,
"O2ZiLv4uU": 1,
"IWtoo1eNy": 1,
"1nMcMvTXl": 1,
"SyrTagaRH": 1,
"UjaUY9vY3": 1,
"jpwOVjZGr": 1,
"ovxuuYULh": 1,
"wMjzeLU0G": 1,
"Scr6U1dXu": 1,
"gjEV9UPec": 1,
"qsPVPunRm": 1,
"dpE7HpqaL": 1,
"hTAXdFp2x": 1,
"XjrSJH02Z": 1,
"l8cdUle33": 1,
"5OPDmb1Im": 1,
"4dYZZjnVz": 1,
"HMpacJ8U1": 1,
"aM7tGSbmu": 1,
"UXe68BRe8": 1,
"7ejVzZCeC": 1,
"5HOF7GHAb": 1,
"h185TqxTS": 1,
"FEzCTBVpS": 1,
"MMK52mf5L": 1,
"JwrLHJ5o4": 1,
"0vbzNof8Z": 1,
"vowkMzWCm": 1,
"PvKgl1Nqp": 1,
"AbfcYnxcu": 1,
"8k20PwPic": 1,
"dXopE4116": 1,
"egnbHMgEF": 1,
"tG0E7Rci7": 1
}
````