如何用 Python 生成随机电话号码?
在本文中,我们将学习如何使用 Python 生成随机电话号码。一般来说,印度的电话号码是 10 位数,以 9、8、7 或 6 开头。
进场:
- We will use the random library to generate random numbers.
- Numbers should contain 10 digits.
- If the first digit starts with 9 or 8 or 7 or 6, we will use the method of randint () .
- The remaining 9 digits will also be generated by randint () method.
例:
The generated random phone numbers would looklike:
9980231467
8726189362
实现:
Python 3
# import module
import random as r
ph_no = []
# the first number should be in the range of 6 to 9
ph_no.append(r.randint(6, 9))
# the for loop is used to append the other 9 numbers.
# the other 9 numbers can be in the range of 0 to 9.
for i in range(1, 10):
ph_no.append(r.randint(0, 9))
# printing the number
for i in ph_no:
print(i, end="")
输出:
8349603502
版权属于:月萌API www.moonapi.com,转载请注明出处