上次做的隨機生成字符串的代碼,稍微顯得有點簡陋,參見上文:
這次做了一個修改版本,此版本比上次的多了一些驗證同時加入了很多驗證的基準類型。
import random
welcom = '歡迎您使用本軟件,本程序的作用是根據用戶輸入的條件生成指定長度指定數量的隨機數'
author = '作者:格子工作室'
email = '郵箱:1831795577@qq.com'
version = '版本:V 1.0.0'
bar = '='*20
br = '\n'
#書寫歡迎信息、作者信息、郵箱、版本等資料
print(bar*4 + br)
print(welcom)
print(author)
print(email)
print(version)
print(br + bar*4)
print(br)
#定義用戶輸入組合
#單純數字 0 - 9
type_num = ['0','1','2','3','4','5','6','7','8','9']
#小寫字母 a - z
type_lowwer_words = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#大寫字母 A - Z
type_upper_words = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
#單純數字 + 小寫字母
type_num_lowwer = type_num + type_lowwer_words
#單純數字 + 大寫字母
type_num_upper = type_num + type_upper_words
#小寫字母 + 大寫字母
type_lowwer_upper = type_lowwer_words + type_upper_words
#數字 + 小寫字母 + 大寫字母
type_all = type_num + type_lowwer_words + type_upper_words
#根據獲取 用戶 給定的數值,得到隨機數組合的結果
def get_random_type():
? ? print('請根據您的需求對需要生成的數碼組合進行選擇!')
? ? print('輸入 1 :僅用數字')
? ? print('輸入 2 :僅用小寫字母')
? ? print('輸入 3 :僅用大寫字母')
? ? print('輸入 4 :用數字 + 小寫字母')
? ? print('輸入 5 :用數字 + 大寫字母')
? ? print('輸入 6 :用小寫字母 + 大寫字母')
? ? print('輸入 7 :用數字 + 小寫字母 + 大寫字母')
? ? get_userType = input('請輸入您的選擇:')
? ? if get_userType.isdigit():
? ? ? ? global user_Type
? ? ? ? #獲取到的數值仍然為Str
? ? ? ? if get_userType == '1' :
? ? ? ? ? ? user_Type = type_num
? ? ? ? elif get_userType == '2' :
? ? ? ? ? ? user_Type = type_lowwer_words
? ? ? ? elif get_userType == '3' :
? ? ? ? ? ? user_Type = type_upper_words
? ? ? ? elif get_userType == '4' :
? ? ? ? ? ? user_Type = type_num + type_lowwer_words
? ? ? ? elif get_userType == '5' :
? ? ? ? ? ? user_Type = type_num + type_upper_words
? ? ? ? elif get_userType == '6' :
? ? ? ? ? ? user_Type = type_lowwer_upper
? ? ? ? elif get_userType == '7' :
? ? ? ? ? ? user_Type = type_all
? ? ? ? else:
? ? ? ? ? ? print(bar)
? ? ? ? ? ? print(br)
? ? ? ? ? ? print('請輸入正確的選擇,選擇范圍 1-6')
? ? ? ? ? ? print(br)
? ? ? ? ? ? get_random_type()
? ? else:
? ? ? ? print(bar)
? ? ? ? print(br)
? ? ? ? print('輸入錯誤:請輸入正確的選擇,選擇范圍 1-6')
? ? ? ? print(br)
? ? ? ? get_random_type()
#調用函數取得用戶輸入資料
get_random_type()
def get_random_Len():
? ? get_Len = input('請輸入您需要生成的隨機數碼的長度: 例如 : 20 ' + br)
? ? if get_Len.isdigit():
? ? ? ? return get_Len
? ? else:
? ? ? ? print(bar)
? ? ? ? print(br)
? ? ? ? print('輸入錯誤:請輸入正確的選擇')
? ? ? ? print(br)
? ? ? ? get_random_Len()
#獲取用戶輸入的要生成的隨機數碼長度
Enter_Len = int(get_random_Len())
#獲取用戶輸入的需要生成的隨機數碼數量
def get_num():
? ? get_num_line = input('請輸入您需要生成的隨機數碼的數量:')
? ? if get_num_line.isdigit():
? ? ? ? return get_num_line
? ? else:
? ? ? ? print(bar)
? ? ? ? print(br)
? ? ? ? print('輸入錯誤,請輸入正確的隨機數碼數量!')
? ? ? ? print(br)
? ? ? ? get_num()
#獲取需要生成的隨機數碼的數量
get_nums = int(get_num())
if (len(user_Type) / Enter_Len) >= 1 :
? ? print('正在處理,請稍等!')
? ? print('處理完成后,您可以打開軟件所在目錄下尋找 Random_list.txt 文件 。 ')
? ? print('請注意:如果要重新生成,請將目錄下的 Random_list.txt 文件刪除,以免造成數據混亂。 ')
? ? for random_x in range(0,get_nums):
? ? ? ? #隨機數生成數碼
? ? ? ? random_line = str(random.sample(user_Type,Enter_Len)).replace(',','').replace('\'','').replace(' ','').replace('[','').replace(']','')
? ? ? ? f = open('Random_list.txt','a')
? ? ? ? f.write(random_line + br)
? ? ? ? f.close()
else:
? ? print('單次數量不滿足數量,現將數值 * 2 ')
? ? #這是未知解決方法的情況下,暫時的折衷辦法,實在腦子不好用,想不出來
? ? for random_x in range(0,get_nums):
? ? ? ? #隨機數生成數碼
? ? ? ? random_line = str(random.sample(user_Type*2,Enter_Len)).replace(',','').replace('\'','').replace(' ','').replace('[','').replace(']','')
? ? ? ? f = open('Random_list.txt','a')
? ? ? ? f.write(random_line + br)
? ? ? ? f.close()
print('處理完成,程序執行退出操作!')