更新使用 choices 函数的测试

This commit is contained in:
YuCheng Hu 2021-03-19 11:05:07 -04:00
parent c3fdd5ac02
commit 2ea753ac1d
No known key found for this signature in database
GPG Key ID: 1E5CBEF8B550FB7D
1 changed files with 7 additions and 2 deletions

View File

@ -5,7 +5,7 @@
# Link Article - https://www.ossez.com/t/python/13398 # Link Article - https://www.ossez.com/t/python/13398
import string import string
from random import choice import random
def random_password(length, printable): def random_password(length, printable):
@ -15,7 +15,7 @@ def random_password(length, printable):
:param int length: 生成随机字符串的数量 :param int length: 生成随机字符串的数量
""" """
return "".join([choice(printable) for x in range(int(length))]) return "".join([random.choice(printable) for x in range(int(length))])
if __name__ == "__main__": if __name__ == "__main__":
@ -39,3 +39,8 @@ for i in range(1, amount + 1):
print('') print('')
for i in range(1, amount + 1): for i in range(1, amount + 1):
print(f" 随机字符串 [Ascii Digits String]: {i} - {repr(random_password(number, string.digits))} ") print(f" 随机字符串 [Ascii Digits String]: {i} - {repr(random_password(number, string.digits))} ")
# choices Function Test
print('')
my_list = ["apple", "banana", "cherry"]
print(random.choices(my_list, weights=[10, 1, 1], k=12))