You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python运营商手机号生成器代码运行异常排查修正

代码问题排查与修正方案

现存错误点

代码运行报错、无法实现预期批量生成号码功能,由以下问题导致:

  • 未导入依赖:使用random.choice前没有导入random模块,运行直接触发NameError
  • 未定义carrier变量:代码直接判断carrier取值,但没有编写用户选择运营商的输入逻辑,运行直接触发变量未定义错误
  • 语法错误:Telstra分支拼接手机号的代码行末尾多写了一个右括号,Python解析阶段就会报语法错误
  • 缩进错误:打印号码、写入文件的逻辑放在for循环外部,无论设置生成多少个号码,最终只会保存最后1个生成结果
  • 文件操作风险:直接调用open()写入后未关闭文件句柄,大概率出现写入内容丢失、文件被占用的问题
  • 冗余逻辑:将用户输入的生成数量先转字符串再转回整数,还重复赋值给三个完全等价的Number1/2/3变量,属于无效代码
  • 分支不全:仅实现了Telstra、Optus两个运营商的生成逻辑,Vodafone无对应处理代码,用户选3时程序无响应
  • 无异常提示:用户输入非1/2/3的无效运营商编号时,程序直接退出无任何提示。

修正后完整代码

以下代码修复了全部上述问题,其中Vodafone号段为占位值,替换为实际号段即可正常使用:

import random

# 运营商选择交互
print("---------------------------------------------")
print("可选运营商:")
print("1. Telstra")
print("2. Optus")
print("3. Vodafone")
carrier = input("请输入对应编号选择运营商:")

# 生成数量输入
count = int(input("How Many Numbers Do You Want To Generate: "))
print("---------------------------------------------")

# 号段配置
num_charset = "0123456789"
Telstra_prefix = ("4001", "4002", "4003", "4004", "4005", "4006", "4007", "4008", "4009", "4601", "4609", "4800", "4901", "4909", "4910", "4914", "4920", "4929", "4070", "4079", "4080", "4089", "4090", "4099", "4170", "4179","4180", "4189", "4190", "4199", "4270", "4279", "4280", "4289", "4290", "4299", "4470", "4479", "4480", "4484", "4971", "4979", "4980", "4989", "4990", "4999")
Optus_prefix = ("4010", "4019", "4020", "4029", "4030", "4039", "4110", "4119", "4120", "4129", "4130", "4139", "4810", "4819", "4340", "4349", "4350", "4359")
# 替换为Vodafone实际号段
Vodafone_prefix = ("test",)

# 号码生成与写入逻辑
if carrier == "1":
    with open("TelstraPhoneNumbers.txt", "a", encoding="utf-8") as f:
        for _ in range(count):
            prefix = random.choice(Telstra_prefix)
            suffix = "".join(random.choice(num_charset) for _ in range(6))
            phone = f"+61{prefix}{suffix}"
            print(phone)
            f.write(f"{phone}\n")
elif carrier == "2":
    with open("OptusPhoneNumbers.txt", "a", encoding="utf-8") as f:
        for _ in range(count):
            prefix = random.choice(Optus_prefix)
            suffix = "".join(random.choice(num_charset) for _ in range(6))
            phone = f"+61{prefix}{suffix}"
            print(phone)
            f.write(f"{phone}\n")
elif carrier == "3":
    with open("VodafonePhoneNumbers.txt", "a", encoding="utf-8") as f:
        for _ in range(count):
            prefix = random.choice(Vodafone_prefix)
            suffix = "".join(random.choice(num_charset) for _ in range(6))
            phone = f"+61{prefix}{suffix}"
            print(phone)
            f.write(f"{phone}\n")
else:
    print("输入无效,请重新运行程序输入1/2/3选择对应运营商")

使用说明

  1. 运行脚本后按控制台提示,输入1/2/3选择目标运营商
  2. 输入需要生成的手机号码数量,回车确认
  3. 程序会自动按照对应运营商号段生成+61开头的手机号,所有号码会逐行打印在控制台,同时追加写入脚本同目录下、以运营商命名的TXT文件中。

内容的提问来源于stack exchange,提问作者Noobaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 09:06:19