1. 程式人生 > >day 09 作業

day 09 作業

import random
# 1.將登陸註冊的程式碼改成函式的寫法
def a(): print("歡迎來到登入介面") name = input("請輸入使用者名稱:") password = input("請輸入密碼:") a = {name: password} for i, j in database.items(): if a == {i: j}: print("登入成功") if a != {i: j}: print("賬號或密碼錯誤,請重新輸入") def b(): while True: print("歡迎來到註冊介面\n不需要註冊輸入:0") name = input("請輸入使用者名稱:") if name == "0": print("退出註冊\n") break password = input("請輸入密碼:") for i in database.keys(): if name == i: print("此賬戶已被註冊\n") break if name != i: database.setdefault(name, password) print("註冊成功") break database = {" ": " "} while True: choice = int(input("網站登入介面\n1:登入\n2:註冊\n3:退出\n請選擇:")) if choice == 1: a() if choice == 2: b() if choice == 3: print("退出成功") break # 2.設計一個函式,統計一個字串中出現頻率最高的字元(單個符號)及其出現次數
str1 = input("請輸入:") def hanshu(): top = 0 count = 0 num = 0 while num < len(str1): if count < str1.count(str1[num]): count = str1.count(str1[num]) top = str1[num] num += 1 print("頻率最高的字元為", '\"', top, '\"') print("出現", count, "次") hanshu() # 3.設計一個函式,根據指定長度生成對應的驗證碼(由數字和大小寫英文字母構成的隨機字串)長度由使用者去輸入。
username = input("請輸入需要生成多少位的驗證碼:") scq = [] def asd(): count = 1 while count <= int(username): a = random.randint(48, 122) if (47 < a < 58) or (64 < a < 91) or (96 < a < 123): scq.append(chr(a)) count += 1 else: continue asd() for i in scq: print(i, end="")