![]() Word = '' #The variable which will hold the random word ascii_uppercase #A constant containing uppercase letters def uppercase_word ( ) : #The function responsible for generating #random words which are in uppercase ascii_lowercase #A constant containing lowercase letters It contains a collection of string constants import random #Python's module for generating random objects import string #Python module for strings. Well, in this section, we will generate random uppercase letters. Then on the last line, I just printed it out. Random_word = lowercase_word() calls the function lowercase_word and the function generates a word and assigns it to the random_word variable. Then the last line, return word returns the generated word. Each time the loop is repeated, the random.choice selects a random letter from the lowercase_letters variable and appends it to the word variable. Then to the next line, word += random.choice(lowercase_letters), this statement is repeated in the 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 □. To the next line, while len(word) != random_word_length: is a while loop. Meaning the results is going to be a random number which is in the range of 1 to 10. In my case, a and b is 1 and 10 respectively. The results is an integer which is stored in the variable random_word_length. Refers to the minimum random number which can be generated and ‘b’ refers to the maximum The random.randint method takes two parameters, ‘a’ and ‘b’. Random_word_length is a variable which is going to hold a random number. The word variable is the variable which is going to hold the random word. This is the function which generates lowercase random words. Let’s move on to the function called lowercase_word. Meaning, it contains the letters a,b,c,…,y,z. The variable lowercase_letters contains all the lowercase letters defined in it. It contains classes which helps us to generate random objects. Then on the second line, I imported the random module. Meaning, it contains lowercase letters, uppercase letters, and other constants defined. Python’s □ string module contains a collection of string constants. On the first line, I imported the string module. Well, after running the code, this is what I got ⬇️ ![]() choice (lowercase_letters ) #Selects a random character on each iteration return word #Returns the word randint ( 1, 10 ) #The random length of the word while len (word ) != random_word_length : #While loop Lowercase word = '' #The variable which will hold the random word ascii_lowercase #A constant containing lowercase letters def lowercase_word ( ) : #The function responsible for generating the word Give the file a name and let’s start coding. Open your favourite IDE and start a new file. So in this section, we are going to generate random words which are lowercase letters. We will start easy and as we progress, things will become a little tricky. As you can see, they are kinda many, don’t worry, I will explain the code in detail. We are going to generate random words in many formats. Meaning you have no idea of the word being generated. ![]() Random words are just a combination of random letters. So after reading the meaning of random, I hope you now have an understanding of random objects. The numbers, 2,655,1,43,4,22 are random because you can not predict the next outcome or possibilities. If something is said to be random, it has no order and does not follow any pattern or combination.įor example, the numbers, 2,4,6…12 are not random because you can predict the possible outcomes because they are even numbers. It’s simply the lack of pattern or predictability in events. So, you are here because you want to learn how to generate random words in Python but do you really know the meaning of random ❓ Determining first and last letters of random word Introduction.
0 Comments
Leave a Reply. |