
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Even Length Series of Random Four-Digit PINs in Python
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
Advertisements