Write a program in Python to generate an even (length) series of random four-digit pin. Get the length from user and ask until it’s valid

PythonPandasServer Side ProgrammingProgramming

The result for generating even length random four-digit pin numbers as,

enter the series size 4
Random four digit pin number series
0    0813
1    7218
2    6739
3    8390

To solve this, we will follow the steps given below −

Solution

  • Create an empty and list and set result as True

  • Set while loop and get the size from the user

  • Set if condition to find the size is even or odd. If the size is odd then assign the result as False and runs the loop until an even number is entered.

l = []
while(True):
   size = int(input("enter the series size"))
   if(size%2==1):
      result = False
  • If the size is an even value then assign the result as True and set it for loop to access the range of size.

result = True
for i in range(size):
  • Generate random four-digit numbers and store it rand_pin.

  • Append rand_pin values to the list then generate a series.

rand_pin = random.sample(num,4)
l.append("".join(rand_pin))
pd.Series(l)
  • If the result value is True the break the look using if condition.

if(result==True):
   break

Example

Let’s understand the below code −

# importing pandas as pd
import pandas as pd
import random,string
num = string.digits
result = True
l = []
while(True):
   size = int(input("enter the series size"))
   if(size%2==1):
      result = False
   else:
      result = True
      for i in range(size):
         rand_pin = random.sample(num,4)
         l.append("".join(rand_pin))
      series = pd.Series(l)
      print("Random four digit pin number series\n",series)
   if(result==True):
      break

Output

enter the series size 3
oops! enter an even number!
enter the series size 5
oops! enter an even number!
enter the series size 4
Random four digit pin number series
0    0813
1    7218
2    6739
3    8390
dtype: object
raja
Published on 12-Feb-2021 11:10:14
Advertisements