Answers for "Generate 10 verification codes of length 4 and print them, e.g.,"

0

Generate 10 verification codes of length 4 and print them, e.g.,

#random package to find random integer
import random

#This method returns a random value of given length
#len - parameter determines the length of the random value
def random_value(len):
    result = ''
    #len decrements with each iteration 
    #Hence the while loop runs len number of times
    while len!=0:
        #we find a boolean of 0 or 1, by generating number %2 which will return 0 or 1.
        char_or_num = (random.randint(0,9))%2
        #If number is 1 , we generate a random integer between 97-122 which is ASCII value of a-z.
        if char_or_num==1:
            letter = chr(random.randint(97,122))
            #append the letter to result variable
            result += letter
        #if remainder is 0, then we generate a number between 0 and 9 .
        else :
            num = random.randint(0,9)
            #append the value to result variable by converting the integer to string using str() method
            result += str(num)
        #decrement the number with each iteration
        len-=1
    #return the string of given length
    return result

#method returns a list of 'num' number of strings of length 'len' 
def generate_random(num , len):
    #empty list to store the strings
    lists = []
    #iterate through the list
    for i in range(num):
        #find the random value and append it to the list
        lists.append(random_value(len))
    #return the list
    return lists

#test case of 10 values , each of length 4
print(generate_random(10,4))

#test case tp generate 5 values fo length 7 each
print(generate_random(5,7))
Posted by: Guest on June-27-2021

Code answers related to "Generate 10 verification codes of length 4 and print them, e.g.,"

Browse Popular Code Answers by Language