随机密码,验证码
s = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890'
code = ''
import random
for i in range(5):
ran = random.randint(0, len(s) - 1)
code += s[ran]
print(code)
user_input = input('请输入验证码:')
if user_input.lower() == code.lower():
print("验证码正确!!!")
else:
print("验证码错误")
石头剪刀布
'''
if 条件1:
条件成立代码1
elif 条件2:
条件成立代码2
else:
条件都不成立代码
'''
import random
name = 'lisi'
passwd = '1234'
user = input('请输入用户:')
password = input('请输入密码:')
# if name == user:
# if password == passwd:
# print('欢迎master')
# else:
# print('密码错误')
# else:
# print('你不是我的master')
if name == user and password == passwd:
print(''' 欢迎master
#######################################
我们开始玩游戏石头剪刀布
#######################################''')
play = int(input('''
请出拳:
0--石头
1--剪刀
2--布
:'''))
computer = int(random.randint(0,2))
print(computer)
if ((play == 0) and (computer == 1)) or ((play ==1) and (computer ==2)) or ((play==2) and (computer==0)):
print("恭喜玩家获胜%s-%s" % (play,computer))
elif play == computer:
print('平局,别走再来嘛%s-%s' % (play,computer))
else:
print('很可惜,你输了%s-%s' % (play,computer))
else:
print('你不是我的master')
文件上传
for i in range(1,4):
#文件上传,只允许上传图片(jpg,png,bmp) #F:\300\跳跃战网\html\web_ad_push\imgs\src.jpg
path = input('请输入文件夹路径:')
p = path.rfind('\\') #将 \ 转义 \\
filename = path[p+1:]
#判断是否是图片类型
print(filename)
if filename.endswith('jpg') or filename.endswith('png') or filename.endswith('bmp'):
print('是图片允许上传')
break
else:
print('该文件不是图片类型,请选择jpg,png,bmp格式上传')
print('您有3次机会,3次错误会直接退出锁定,现在是第{}次'.format(i))
else:
print('很遗憾,您已经输错3次,请解锁再来')
冒泡排序
import random
#算法 冒泡排序
list1 = []
for i in range(4):
num = random.randint(1,50)
list1.append(num)
print(list1)
list2=sorted(list1)
print(list2)
#自定义
for i in range(len(list1)):
for j in range(i+1,len(list1)):
if list1[i] <= list1[j]:
#快速交换
list1[i],list1[j] = list1[j],list1[i]
print(list1)
print(list1)
用户注册
print('-'*40)
print('\t\t欢迎来到智能招聘用户注册')
print('-'*40)
database = []
while True:
username = input('请输入用户名:')
password = input('请输入密码:')
repassword = input('请再次输入密码:')
email = input('输入邮箱地址:')
phone = input('输入手机号')
#定义一个字典
user = {}
# 将信息保存到字典
user['username'] = username
if password == repassword:
user['password'] = password
else:
print('密码不一致,请重新注册')
continue
user['email'] = email
user['phone'] = phone
#保存到数据库
database.append(user)
answer = input('是否继续注册?(y/n):')
if answer != 'y':
break
print(database)
王者荣耀
import random
print('*'*40)
print('\t\t\t欢迎来到王者荣耀')
print('*'*40)
role = input('''1,射手 2,坦克 3,辅助 4,法师 5,战士'
请选择游戏人物:''')
coins = 1000
print(f'''勇士你来啦,今天又是充满希望的一天
您当前选择的职业是{role},当前金币是{coins}''')
#保存自己武器的库
weapon_list = []
while True:
choice = int(input('1,购买武器\n2,对战\n3,删除武器\n4,查看武器\n5,退出游戏\n请选择:'))
if choice == 1:
print('欢迎来到武器库')
weapons = [['小刀',500],['木盾',500],['步鞋',500]]
# 打印出所有的武器名和价格
for weapon in weapons:
print(weapon[0],weapon[1],sep=' ')
#提示输入要购买的武器
weaponname = input('选择你要购买的武器:')
#判断武器列表是否小于7个
if len(weapon_list) < 7:
#遍历武器库,让所有的武器个价格变成单个列表
for weapon in weapons:
# 判断购买的武器是否在武器库
if weaponname in weapon[0]:
#判断金币数是否够买武器
if coins >= weapon[1]:
coins -= weapon[1]
weapon_list.append(weapon[0])
print(f'购买成功,剩余金币{coins}')
break
else:
print('金币不足,快去打仗去')
break
else:
print('输入的武器名错误')
elif choice == 2:
print('勇士进入战场吧')
#选择武器
print('你拥有的武器如下:')
for weapon in weapon_list:
print(weapon)
while True:
weaponname = input('请选择武器:')
if weaponname in weapon_list:
ran1 = random.randint(1,100)
computer = random.randint(1,100)
if ran1 > computer:
coins += 1000
print(f'恭喜胜利,金币加1000,你现在拥有金币{coins}')
elif ran1 < computer:
coins -= 100
print(f'很遗憾你失败了,金币减少100,你现在拥有金币{coins}')
else:
print('平局')
paly = input('是否开启下一次对局(y/n)?:')
if paly == 'yes' or paly == 'y':
print('勇士赶快开启下一局')
elif paly == 'no' or paly == 'n':
print('很遗憾,勇士期待下次对战')
break
else:
print('输入错误')
break
else:
print('你还没有武器,请先购买')
elif choice == 3:
#删除武器
print('仓库满了,删除武器')
if len(weapon_list) > 0:
print('你拥有的武器如下:')
for weapon in weapon_list:
print(weapon)
weaponname = input('请选择要删除的武器名称:')
while True:
if weaponname in weapon_list:
weapon_list.remove(weaponname)
for weapon in weapons:
if weaponname == weapon[0]:
coins += weapon[1]
print(f'你当前的金币数{coins}')
break
break
else:
print('武器库为空')
elif choice == 4:
print('你拥有的武器如下:')
for weapon in weapon_list:
print(weapon)
elif choice == 5:
answer = input('勇士确定要离开游戏吗(y/n)?:')
if answer == "yes" or answer == 'y':
break
else:
print('欢迎再次来到王者荣耀,今天又是充满希望的一天,来自赛利亚的关爱')
else:
print('输入错误,请重新输入!')
函数相互调用
islogin = False #判断用户是否登录,默认false,没有登陆
import random
def yanzm(n):
g= '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
code = ''
for i in range(n):
ran = random.randint(0,len(g)-1)
code += g[ran]
return code
def login():
global islogin
username = input('请输入用户名:')
password = input('请输入密码:')
code = yanzm(5)
print(f'验证码是{code}')
code1 = input('输入验证码:')
if code.lower() == code1.lower():
if username == 'zilong' and password == '123456':
print('登陆成功')
return True
# # 函数调用,添加商品到购物车
# add_shoppingcart('生命水')
else:
print('登陆失败')
return False
else:
print('验证码输入有误!')
login()