Python语言编写有趣练习题!
1. 登录作业:
写一个登录程序,登录成功之后,提示XXX欢迎登录,登录失败3次后,提示账户锁定
username = "admin"
passwd = "1234"
count =0
_username = str(input("请输入用户名:"))
while count < 3:
_passwd = str(input("请输入密码:"))
if _username == username and _passwd == passwd :
print(username,'欢迎登录')
break
else:
if count < 2:
print("输入错误,请检查后再一次输入")
else:
print("由于你输入的错误次数过多,登录已经被锁定")
count += 1
if count == 3:
f =open("lock.txt","a",encoding="utf-8")
f.write("\n")
f.write(_username)
2. 判断密码是否安全
设计一个密码是否安全的检查函数。
密码安全要求:
1.要求密码为6到20位,
2.密码只包含英文字母和数字
import re
def check_code(code):
while True:
if len(code) < 6 or len(code) > 20:
return '密码长度不足6-20位'
break # 不用break将是死循环
else:
for i in code:
s = ord(i) in range(97, 123) or ord(i) in range(65, 91) or ord(i) in range(48, 59)
if not s:
return '密码只能包含英文字母和数字,不能填入其他字符'
break
else:
return '密码安全'
print(check_code('555555'))
3. 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
分析:
- 可填在百位、十位、个位的数字都是1、2、3、4
- 组成所有的排列后再去掉不满足条件的排列
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print (i,j,k)
4. 打印楼梯,同时在楼梯上方打印两个笑脸
print("^_^",end='')
for i in range(1,11):
for j in range(1,i):
print('===',end='\t')
print()
延伸一下,很有趣!
import turtle
# 画矩形立方体
def draw_cube(i):
turtle.begin_fill()
turtle.color("black")
turtle.goto(i, i * 3)
turtle.goto(100 + i, i * 3)
turtle.goto(100 + i, 20 + i * 3)
turtle.goto(i, 20 + i * 3)
turtle.goto(i, i * 3)
turtle.end_fill()
turtle.penup()
turtle.goto(i, 20 + i * 3)
turtle.pendown()
turtle.goto(10 + i, 30 + i * 3)
turtle.goto(110 + i, 30 + i * 3)
turtle.goto(110 + i, 10 + i * 3)
turtle.goto(100 + i, i * 3)
turtle.penup()
turtle.goto(100 + i, 20 + i * 3)
turtle.pendown()
turtle.goto(110 + i, 30 + i * 3)
# 画笑脸
def draw_smile_face(x, y):
turtle.goto(x + 50, y)
turtle.pensize(1.5)
# 脸部
turtle.circle(20)
turtle.penup()
# 眼睛
turtle.goto(x + 40, y + 20)
turtle.pendown()
turtle.begin_fill()
turtle.color("black")
turtle.circle(3)
turtle.end_fill()
turtle.penup()
turtle.goto(x + 60, y + 20)
turtle.pendown()
turtle.begin_fill()
turtle.color("black")
turtle.circle(3)
turtle.end_fill()
turtle.penup()
# 嘴巴
turtle.goto(x + 45, y + 10)
turtle.pendown()
turtle.right(90)
turtle.pensize(2)
turtle.circle(5, 180)
def main():
turtle.speed(2)
for i in range(0, 100, 10):
draw_cube(i)
draw_smile_face(100, 300)
turtle.hideturtle()
time.sleep(3)
main()
5. 打印心形
import time
sentence = "Dear, I love you forever!"
for char in sentence.split():
allChar = []
for y in range(12, -12, -1):
lst = []
lst_con = ''
for x in range(-30, 30):
formula = ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3
if formula <= 0:
lst_con += char[(x) % len(char)]
else:
lst_con += ' '
lst.append(lst_con)
allChar += lst
print('\n'.join(allChar))
time.sleep(1)
6. 9*9乘法表
for i in range(1,10):
for j in range(1,i+1):
print(str(j) + str("*") + str(i)+"=" + str(i*j),end="\t")
print()