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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM