🌱练习点
数据拼接、循环和判断。
🌱代码
# 练习代码1
import random as ra
# 预设密码
user_pd = 8844
i1 = int(input("请输入你破解密码的次数:"))
i = 1
while i<=i1:
p1 = str(ra.randint(0, 9))
p2 = str(ra.randint(0, 9))
p3 = str(ra.randint(0, 9))
p4 = str(ra.randint(0, 9))
p = int(p1 + p2 + p3 + p4)
if p == user_pd:
print("经过",i,"次,密码破解成功!")
break
else:
if i !=i1:
print("正在进行第",i,"次破解。")
else:
print("经过",i1,"次尝试,未能破解出密码,破解次数不足,程序结束。")
i += 1
# 练习代码2
import random as ra
user_pd = 8844
# 预设密码
i = 1
# 预设循环次数的变量
while True:
p1 = str(ra.randint(0, 9))
p2 = str(ra.randint(0, 9))
p3 = str(ra.randint(0, 9))
p4 = str(ra.randint(0, 9))
p = int(p1 + p2 + p3 + p4)
if p == user_pd:
print("经过", i, "次,密码破解成功!")
break
else:
print("正在进行第",i,"破解。")
i += 1