我试图生成多个电话号码到一个列表



我能够正确地生成我想要的数字,但我也想做一个自定义输入,这样用户就可以生成精确数量的数字,而不仅仅是一组数字。

if menu == "1":
code = input("")
if code == "":
countrycode = input("Please Enter Country Code: ")
areacode = input("Enter area Code: ")
city = (random.randint(200,999))    
last = (random.randint(1000,9999))
number = ("+"+countrycode+areacode+str(city)+str(last))

print(number)

要做到这一点,您可以使用while True循环,每次他们想要输入电话号码时都接受输入。如果它们输入了你不想要的东西,跳出while True循环。否则,你就接收电话号码。要保存这组数字,只需将它们添加到数组中。

编辑你的代码:

if menu == "1":
while(True):
code = input("")
if code == "":
countrycode = input("Please Enter Country Code: ")
areacode = input("Enter area Code: ")
city = (random.randint(200,999))    
last = (random.randint(1000,9999))
number = ("+"+countrycode+areacode+str(city)+str(last))

print(number)
else:
break



我不确定我完全理解你的问题,但我会试一试:

import random

class Data():
def __init__(self) -> None:
country_code = ""
area_code = ""
city = ""
last = ""

def __str__(self):
return f"+{self.country_code}-{self.area_code}-{self.city}-{self.last}"
def main():
number_of_entries = int(input("How Many Entries: "))

list_of_information = []
for _ in range(0, number_of_entries):
new_data = Data()
new_data.country_code = input("Please Enter Country Code: ")
new_data.area_code = input("Enter area Code: ")
new_data.city = str(random.randint(200,999))  
new_data.last = str(random.randint(1000,9999))
list_of_information.append(new_data)

for entry in list_of_information:
print(entry)
if __name__ == '__main__':
main()

示例:

How Many Entries: 2
Please Enter Country Code: 1
Enter area Code: 604
Please Enter Country Code: 1
Enter area Code: 30
+1-604-394-5276
+1-30-840-8783

相关内容

  • 没有找到相关文章

最新更新