Bài đăng này đi qua cách tạo ra một từ hoặc chữ cái ngẫu nhiên trong Python. Cài đặt từ ngẫu nhiên và pyyaml:Cài đặt
pip3 install random-word pyyaml
Pyyaml là bắt buộc nếu không bạn sẽ gặp lỗi:
ModuleNotFoundError: No module named 'yaml'
Cách sử dụng
Tạo một từ ngẫu nhiên:
from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
Xem tài liệu gói để biết thêm thông tin.
Thử nghiệm
Replit:
Thư ngẫu nhiên
Nhận một chữ cái ngẫu nhiên từ bảng chữ cái:
from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
Thử nghiệm
Replit:
Thư ngẫu nhiên
#*A handy python password generator*
Đây là đầu ra
import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '[', ']', '*', '+']
print["Welcome to the Python Password Generator!"]
l= int[input["How many letters would you like in your password?\n"]]
s = int[input[f"How many symbols would you like?\n"]]
n = int[input[f"How many numbers would you like?\n"]]
sequence = random.sample[letters,l]
num = random.sample[numbers,n]
sym = random.sample[symbols,s]
sequence.extend[num]
sequence.extend[sym]
random.shuffle[sequence]
password = ''.join[[str[elem] for elem in sequence]]#listToStr
print[password]
12 phút đọc
Thứ Hai Mar 29 2021Mục lục
- Giới thiệu
- Từ ngẫu nhiên
- Tạo ra các từ viết thường ngẫu nhiên
- Tạo các từ chữ hoa ngẫu nhiên
- Tạo ra trường hợp hỗn hợp các từ ngẫu nhiên
- Kiểm soát độ dài của từ ngẫu nhiên
- Xác định các chữ cái đầu tiên và cuối cùng của từ ngẫu nhiên
Giới thiệu
Từ ngẫu nhiên
If you don’t, lemme tell ya. It’s simply the lack of pattern or predictability in events. If something is said to be random, it has no order and does not follow any pattern or combination.
For example, the numbers, 2,4,6…12 are not random because you can predict the possible outcomes because they
are even numbers.
The numbers, 2,655,1,43,4,22 are random because you can not predict the next outcome or possibilities.
Từ ngẫu nhiên
Tạo ra các từ viết thường ngẫu nhiên
Random words are just a combination of random letters. Meaning you have no idea of the word being generated.
We are going to generate random words in many formats. As you can see, they are kinda many, don’t worry, I will
explain the code in detail.
We will start easy and as we progress, things will become a little tricky.
Tạo ra các từ viết thường ngẫu nhiên
Tạo các từ chữ hoa ngẫu nhiên
Give the file a name and let’s start coding.
import string #Python module for strings. It contains a collection of string constants
import random #Python's module for generating random objects
lowercase_letters = string.ascii_lowercase #A constant containing lowercase letters
def lowercase_word[]: #The function responsible for generating the word
lowercase word = '' #The variable which will hold the random word
random_word_length = random.randint[1,10] #The random length of the word
while len[word] != random_word_length: #While loop
word += random.choice[lowercase_letters] #Selects a random character on each iteration
return word #Returns the word
random_word = lowercase_word[]
Tạo ra trường hợp hỗn hợp các từ ngẫu nhiên
ModuleNotFoundError: No module named 'yaml'
2Kiểm soát độ dài của từ ngẫu nhiên
On the
first line, I imported the string module. Python’s 🐍 string module contains a collection of string constants. Meaning, it contains lowercase letters, uppercase letters, and other constants defined. Then on the second line, I imported the random module. It contains classes which helps us to generate random objects.
Let’s jump to line 4. The variable
ModuleNotFoundError: No module named 'yaml'
3 contains all the lowercase letters defined in it. Meaning, it contains the letters a,b,c,…,y,z.Let’s move on to the function called
ModuleNotFoundError: No module named 'yaml'
4. This is the function which generates lowercase random words. The ModuleNotFoundError: No module named 'yaml'
5 variable is the variable which is going to hold the random word. Initially, it’s always empty.ModuleNotFoundError: No module named 'yaml'
6 is a variable which is going to hold a random number. The ModuleNotFoundError: No module named 'yaml'
7 method takes two parameters, ‘a’ and ‘b’. The ‘a’ is a number whichrefers to the minimum random number which can be generated and ‘b’ refers to the maximum
random number which can be generated.
The results is an integer which is stored in the variable
ModuleNotFoundError: No module named 'yaml'
6. In my case, a and b is 1 and 10 respectively. Meaning the results is going to be a random number which is in the range of 1 to 10.To the next line,
ModuleNotFoundError: No module named 'yaml'
9 is a while loop.Let me explain, it means while the length of the word variable is not equal to the random number, it should keep on generating random letters 🆒 . Then to the next line,
from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
0, this statement is repeated in the loop. Each time the loop is repeated, the from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
1 selects a
random letter from the lowercase_letters variable and appends it to the word variable. Notice I used from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
2 not from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
3.Then the last line, return word returns the generated word.
from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
4 calls the function lowercase_word and the function generates a word and assigns it to the random_word variable.Then on the last line, I just printed it out.
Tạo các từ chữ hoa ngẫu nhiên
Tạo ra trường hợp hỗn hợp các từ ngẫu nhiên
import string #Python module for strings. It contains a collection of string constants
import random #Python's module for generating random objects
lowercase_letters = string.ascii_lowercase #A constant containing lowercase letters
uppercase_letters = string.ascii_uppercase #A constant containing uppercase letters
def uppercase_word[]: #The function responsible for generating #random words which are in uppercase
word = '' #The variable which will hold the random word
random_word_length = random.randint[1,10] #The random length of the word
while len[word] != random_word_length: #While loop
word += random.choice[uppercase_letters]
return word
Kiểm soát độ dài của từ ngẫu nhiên
Tạo ra trường hợp hỗn hợp các từ ngẫu nhiên
Kiểm soát độ dài của từ ngẫu nhiên
import string #Python module for strings. It contains a collection of string constants
import random #Python's module for generating random objects
lowercase_letters = string.ascii_lowercase #A constant containing lowercase letters
uppercase_letters = string.ascii_uppercase #A constant containing uppercase letters
letters = string.ascii_letters #A contstant containing all uppercase and lowercase letters
def mixedcase_word[]: #The function responsible for generating #random words which are in uppercase
word = '' #The variable which will hold the random word
random_word_length = random.randint[1,10] #The random length of the word
while len[word] != random_word_length: #While loop
word += random.choice[letters]
return word
Xác định các chữ cái đầu tiên và cuối cùng của từ ngẫu nhiên
We only defined a new variable
from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
9s which contains all uppercase and lowercase letters combined.Then, we replaced the code
from random_word import RandomWords
random_words = RandomWords[]
print[random_words.get_random_word[]]
7 with from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
1 and that was all.Kiểm soát độ dài của từ ngẫu nhiên
Điều này rất hữu ích nếu bạn muốn từ ngẫu nhiên có độ dài cụ thể. Chúng tôi sẽ sử dụng chức năng
ModuleNotFoundError: No module named 'yaml'
4 nhưng bạn có thể sử dụng bất kỳ chức năng nào trong ba chức năng.import string #Python module for strings. It contains a collection of string constants
import random #Python's module for generating random objects
lowercase_letters = string.ascii_lowercase #A constant containing lowercase letters
uppercase_letters = string.ascii_uppercase #A constant containing uppercase letters
letters = string.ascii_letters #A contstant containing all uppercase and lowercase letters
def lowercase_word[number = 5]: #The function responsible for generating #random words which are in uppercase word = '' #The variable which will hold the random word
while len[word] != number: #While loop
word += random.choice[lowercase_letters]
return word
Vì vậy, hãy để tôi giải thích, tôi đã sửa đổi chức năng
ModuleNotFoundError: No module named 'yaml'
4. Hãy cùng nhìn vào những thay đổi. Hàm của chúng tôi hiện chấp nhận một tham số được gọi là số, như bạn có thể thấy, nó đã được khởi tạo với một giá trị, 5. Lý do là, nếu bạn chọn không cung cấp một số, nó sử dụng giá trị mặc định trong tham số. Khi gọi hàm, nó sẽ giống như from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
4 này với số mong muốn của bạn trong ngoặc. Độ dài của từ được tạo sẽ bằng chiều dài của số bạn cung cấp. Phần còn lại của mã là khá tự giải thích.Our function now accepts a parameter called number, As you can see, it is already initialized with a value, 5. The reason is that, if you choose not to provide a number, it uses the default value in the parameter.
In calling the function, it will be like this
from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
4 with your desired number being in the brackets.The length of the generated word will be equal to the length of the number you provided. The rest of the code is pretty self-explanatory.
Xác định các chữ cái đầu tiên và cuối cùng của từ ngẫu nhiên
Phần này thực sự đơn giản, chúng tôi sẽ tạo ra một hàm sẽ khiến chúng tôi chỉ định chữ cái đầu tiên và cuối cùng của từ ngẫu nhiên.
ModuleNotFoundError: No module named 'yaml'
0Hàm được gọi là
ModuleNotFoundError: No module named 'yaml'
4, nó giống như cái đầu tiên, chỉ sửa đổi nó một chút. Vì vậy, chức năng của chúng tôi có hai tham số, First_letter và Last_letter. Ban đầu, chúng được đặt thành không ai, vì nó đã thắng được bắt buộc để bạn chỉ định các chữ cái đầu tiên và cuối cùng.Vì vậy, trong dòng đầu tiên của chức năng của chúng tôi, có một biến
from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
6 chứa một số ngẫu nhiên và nó sẽ là độ dài của từ ngẫu nhiên của chúng tôi. Tiếp theo, chúng tôi đã khai báo biến ModuleNotFoundError: No module named 'yaml'
5 sẽ giữ từ ngẫu nhiên. Sau đó, tuyên bố tiếp theo from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
8. Điều này có nghĩa là, nếu giá trị của tham số First_letter không phải là không, [có nghĩa là bạn đã cung cấp một giá trị] thì nó nên gán giá trị của tham số cho từ biến, vì mã này chạy trước khi các chữ cái được tạo, nó sẽ trở thành chữ cái đầu tiên. Sau đó, dòng tiếp theo, sẽ chỉ định giá trị của tham số cho từ biến chỉ khi giá trị của First_letter không phải là không có.Next, we declared the variable
ModuleNotFoundError: No module named 'yaml'
5 which will hold the random word. Then the next statement from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
8. This means that, if the value of the parameter first_letter is not None, [Means you have provided a value] it should assign the value of the parameter to the
variable word, since this code runs before the letters are generated, it then becomes the first letter.Then the next line, will assign the value of parameter to the variable word only if the value of first_letter is not None.
Đối với dòng tiếp theo,
from string import ascii_lowercase
from random import randrange
print[ascii_lowercase[randrange[len[ascii_lowercase]]]]
9 là vòng lặp trong đó các chữ cái ngẫu nhiên được tạo. Sau đó, dòng tiếp theo là nơi nó có một chút khó khăn. #*A handy python password generator*
0. Được rồi, hãy để Lôi lấy nó như thế này. Nó có nghĩa là, nếu độ dài của biến từ bằng số ngẫu nhiên -1, thì để đơn giản hóa nó nhiều hơn, điều đó có nghĩa là, trước khi bạn gán chữ cái cuối cùng, thì đó là dòng tiếp theo sẽ kiểm tra xem giá trị của tham số #*A handy python password generator*
1 không phải là không có. Nếu không phải là không, nó sẽ gán giá trị ở đó cho từ #*A handy python password generator*
2 thay vì tạo một chữ cái mới. Sau đó, dòng tiếp theo, sẽ trả lại từ. Vì vậy, hãy để Lừa xem những cách mà chúng ta có thể sử dụng chức năng.Then the next line is where it’s a little bit tricky.
#*A handy python password generator*
0. Okay, let’s take it like this. It means, if the length of the word variable is equal to the random number -1,
…To simplify it more, it means, “before you assign the last letter,…”
Then the next line will check if the value of the parameter
#*A handy python password generator*
1 is not None. If it is not None, it will assign the value there to the word #*A handy python password generator*
2 instead of generating a new letter.Then the next line, will return the word.
So let’s see ways which we can use the function.
ModuleNotFoundError: No module named 'yaml'
1