1. 程式人生 > 其它 >Python自動化測試(八)

Python自動化測試(八)

#!/usr/bin/python3
# !coding:UTF-8

# 1、結合函式的返回值編寫案例
def login(username,password):
    if username=="admin" and password=="123456":
        return "panduan"
def profile(token):
    if token=="panduan":
        print("顯示個人主頁")
    else:
        print("請登入")
profile(token=login(username="admin",password="
123456")) # 2、在一個Python的檔案中,當全域性變數名稱與區域性變數名稱一致的時候,在Python檔案中呼叫,那個優先順序高?在函式內部,那個優先順序高? # 優先順序:在函式內部的區域性變數高於全域性變數 # 在函式內部使用全域性變數的時候,可以使用關鍵字global來申明 # 3、結合hashlib編寫一個md5的加密的案例 import hashlib import time from urllib import parse def sign(): dict1={"name":"admin","age":"20","nowTime":time.time()} data
=sorted(dict1.items(),key=lambda item:item[0]) data=parse.urlencode(data) # urlencode()key-value鍵值對轉換成所需格式,返回如a=1&b=2字串 m=hashlib.md5() m.update(data.encode('utf-8')) print(m.hexdigest()) sign() # 4、對字典dict1={"name":"wuya","age":18,"work":"測試工程師","salary":1990}進行ascll碼的排序 dict1={"
name":"wuya","age":18,"work":"測試工程師","salary":1990} # sorted()對所有物件排序操作 print(sorted(dict1.items(),key=lambda key:key[0])) # 5、列表lists=["Go","Pyhton","Java","Net"]進行迴圈輸出 lists=["Go","Pyhton","Java","Net"] while True: for item in lists: print(item) break # 6、往列表裡面新的元素,會使用到哪些方法,結合案例程式碼來舉例 lists.append("hello") print(lists) # 7、列表與元組的區別是什麼? # 元組是不可變的 不能修改,不可增加也不可刪除 # 列表是可變的 可以增加,也可以把已有的物件刪除 # 8、break怎麼理解?結合案例程式碼說明 # break跳出整個迴圈 str1="歡迎回來" while True: for item in str1: print(item) break # 9、continue怎麼理解?結合案例程式碼說明 # continue跳出本次迴圈 for i in range(0,3): score=int(input('輸入學生成績:\n')) if score>=30 and score<60: print('成績不合格') elif score>=60 and score<=100: print('成績合格') elif score<30: print('成績差') else:continue # 10、結合函式形式,編寫一個登入註冊的案例 import json USER_LOGIN={'isLogin':False} def register(): # 註冊 username=input('請使用者使用者名稱:\n') password=input('請輸入密碼:\n') temp=username+'|'+password with open('login.txt','w') as f: # with as主要用於檔案的讀寫操作,省關閉檔案操作 f.write(temp) # temp暫存臨時資料夾 register() def login(): # 登入 username=input('請使用者使用者名稱:\n') password=input('請輸入密碼:\n') lists=None with open('login.txt') as f: lists=f.read() lists=lists.split('|') if username==lists[0] and password==lists[1]: USER_LOGIN['isLogin']=True USER_LOGIN['nick']='無涯' print(USER_LOGIN) else: print('請輸入正確的賬戶和密碼') login()