python3的一些練習題

wadeson發表於2017-12-02

下面是最近寫的一些python3版本的一些練習題:

1、4+44+444+4444+.....+=?

root@wadeson:~/study_for_python/homework# cat aa.py
#!/usr/bin/python3

n = int(input("please input a number: "))
init_num = int(input("pleasr input a init number: "))
total = 0
num1 = 0

for i in range(n):
    num1 = num1 + (10**i)*init_num
    total = total + num1

print(total)

執行結果:

root@wadeson:~/study_for_python/homework# python3 aa.py
please input a number: 3
pleasr input a init number: 4
492

2、九九乘法表:

root@wadeson:~/study_for_python/circle# cat while_about_nine2.py
#!/usr/bin/python3

num1 = 1
while num1 <= 9:
    num2 = 1
    while num2 <= 10 - num1:          # num2從9開始依次減小
        print("%d * %d = %d" % (num2,num1,num1*num2), end=" ")       # num2總是從1開始迴圈,並且迴圈到num1的值,end=" "表示不換行,空格間隔
        num2 = num2 + 1
    print()                # 作用是換行
    num1 = num1 + 1

執行如下:

root@wadeson:~/study_for_python/circle# python3 while_about_nine2.py
1 * 1 = 1 2 * 1 = 2 3 * 1 = 3 4 * 1 = 4 5 * 1 = 5 6 * 1 = 6 7 * 1 = 7 8 * 1 = 8 9 * 1 = 9
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 4 * 2 = 8 5 * 2 = 10 6 * 2 = 12 7 * 2 = 14 8 * 2 = 16
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 4 * 3 = 12 5 * 3 = 15 6 * 3 = 18 7 * 3 = 21
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 5 * 4 = 20 6 * 4 = 24
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21
1 * 8 = 8 2 * 8 = 16
1 * 9 = 9

或者如下:

root@wadeson:~/study_for_python/circle# cat while_about_nine.py
#!/usr/bin/python3

num1 = 1
while num1<=9:
    num2 = 1
    while num2 <= num1:   # num2依次從1開始增大到9
        print("%d * %d = %d" % (num2,num1,num1*num2), end=" ")
        num2 = num2 + 1
    print()
    num1 = num1 + 1
root@wadeson:~/study_for_python/circle# python3 while_about_nine.py
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81

3、列印*符號:

root@wadeson:~/study_for_python/circle# cat while_2.py
#!/usr/bin/python3


line = 4
num = line
while line > 0:
    num1 = 0
    while num1 <= num - line:
        print("*", end="")
        num1 = num1 + 1

    print("")
    line = line - 1

執行結果如下:

root@wadeson:~/study_for_python/circle# python3 while_2.py
*
**
***
****
root@wadeson:~/study_for_python/circle# cat while_1.py
#!/usr/bin/python3

num = 1
line = 4

while num <= line:
    num1 = 0
    while num1 <= line - num:
        print("*", end="")
        num1 = num1 + 1

    print("")
    num = num + 1
root@wadeson:~/study_for_python/circle# python3 while_1.py
****
***
**
*

4、驗證賬戶資訊,如果是鎖住的賬戶,則直接登入失敗;如果沒有註冊的賬戶,請先進行註冊;如果輸入賬號密碼錯誤三次則鎖住該賬戶;如果登入成功,則列印成功資訊

root@wadeson:~/study_for_python/homework# cat authenticate_user_land.py
#!/usr/bin/python3
import getpass

user_list = {}
lock_user = []

# 讀取檔案中的的賬號和密碼儲存在使用者字典中
with open("user_list.txt","r") as f1:
    for i in f1.readlines():
        (user,passwd) = i.strip().split()
        user_list[user] = passwd

# 讀取鎖使用者檔案中的user儲存在列表中
with open("lock_user.txt","r") as f2:
    for i in f2.readlines():
        user = i.strip()
        lock_user.append(user)


user_get=input("please input user:")
passwd_get=getpass.getpass("please input password:")
T = 0

while T < 2:
    # 當輸入的名字存在於鎖使用者中時,直接退出
    if user_get in lock_user:
        print("your user %s has been locked,please contact administrator" % user_get)
        break
    # 當輸入的使用者不存在鎖使用者中:1,賬號密碼進行判斷三次  2,賬號不存在
    else:
        if  user_get not in user_list.keys():
            print("user %s is not exist,please register!" % user_get)
            break
        else:
            if passwd_get == user_list[user_get]:
                print("welcome to %s" % user_get)
                break
            else:
                print("your password is wrong,please try again")
                T = T + 1
    user_get=input("please input user:")
    passwd_get=getpass.getpass("please input password:")

else:
    print("your user %s has been locked!" % user_get)
    lock_user.append(user_get)
    with open("lock_user.txt","w") as f_lock:
        for i in lock_user:
            f_lock.write("%s\n" % i)
root@wadeson:~/study_for_python/homework# cat lock_user.txt
user1
wadeson
root@wadeson:~/study_for_python/homework# cat user_list.txt
wadeson    redhat
jsonhc     redhat
root@wadeson:~/study_for_python/homework# python3 authenticate_user_land.py
please input user:wadeson
please input password:
your user wadeson has been locked,please contact administrator
L1 = [1, 2, 3, 4, 5]
L2 = ["a", "b", "c", "d", "e"]
L = ['1a', '2b', '3c', '4d', '5e']
L1 = [1, 2, 3, 4, 5]
L2 = ["a", "b", "c", "d", "e"]
L = []
i = 0
j = 0
while i < len(L1):
    while j < len(L2):
        t = "%s%s" % (L1[i], L2[j])
        L.append(t)
        j = j + 1
        i = i + 1
print(L)

持續更新中

相關文章