Home > Enterprise >  Phone Number Generator ignoring user input
Phone Number Generator ignoring user input

Time:06-27

I'm quite new to Python.

#Amount of numbers wanting to be generated
numbers = str(int(input("How Many Numbers Do You Want To Generate: ")))
Number1 = int(numbers)
Number2 = int(numbers)
Number3 = int(numbers)

print("---------------------------------------------")

#generator values
gentype = "0123456789"

Telstra = "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 = "4010", "4019", "4020", "4029", "4030", "4039", "4110", "4119", "4120", "4129", "4130", "4139", "4810", "4819", "4340", "4349", "4350", "4359"
Vodafone = "test"

#generator code 
if carrier == "1": 
    for i in range(Number1):
     generatestarted1 = random.choice(Telstra)
     generatestarted2 = random.choice(gentype)
     generatestarted3 = random.choice(gentype)
     generatestarted4 = random.choice(gentype)
     generatestarted5 = random.choice(gentype)
     generatestarted6 = random.choice(gentype)
     generatestarted = (" 61" generatestarted1 generatestarted2 generatestarted3 generatestarted4 generatestarted5 generatestarted6))
    print(generatestarted)
    savenumbers = open("TelstraPhoneNumbers.txt","a").write(generatestarted "\n")
elif carrier == "2":
    for j in range(Number2):
     generatestarted1 = random.choice(Optus)
     generatestarted2 = random.choice(gentype)
     generatestarted3 = random.choice(gentype)
     generatestarted4 = random.choice(gentype)
     generatestarted5 = random.choice(gentype)
     generatestarted6 = random.choice(gentype)
     generatestarted = (" 61" generatestarted1 generatestarted2 generatestarted3 generatestarted4 generatestarted5 generatestarted6)
    print(generatestarted)
    savenumbers = open("OptusPhoneNumbers.txt","a").write(generatestarted "\n")

Output:

enter image description here

CodePudding user response:

Your problem is simply a case of indentation. Your print(generatestarted) statement is outside the for loop. Python relies on indentation to segregate code blocks, and by looking at the code you have pasted, assuming you pasted it it as is, will only show the last number that was generated. By copying your code to my Python editor, I was able to reproduce your issue, and simply indenting print(generatestarted) with a single space solved the probelm:

for i in range(Number1):
    generatestarted1 = random.choice(Telstra)
    generatestarted2 = random.choice(gentype)
    generatestarted3 = random.choice(gentype)
    generatestarted4 = random.choice(gentype)
    generatestarted5 = random.choice(gentype)
    generatestarted6 = random.choice(gentype)
    generatestarted = (" 61" generatestarted1 generatestarted2 generatestarted3 generatestarted4 generatestarted5 generatestarted6)
    print(generatestarted)
savenumbers = open("TelstraPhoneNumbers.txt","a").write(generatestarted "\n")

Output:

How Many Numbers Do You Want To Generate: 2
---------------------------------------------
 61490902124
 61497956859

CodePudding user response:

From what I see in your code, your problem is arising with the for loop you wrote, specifically the fact that

print(generatestarted)

is outside of your for-loop, causing only 1 number to be printed. I would also advise that you revise this line of code:

generatestarted2 = random.choice(gentype)
generatestarted3 = random.choice(gentype)
generatestarted4 = random.choice(gentype)
generatestarted5 = random.choice(gentype)
generatestarted6 = random.choice(gentype)

as it may be slightly repetitive and when simplified can be easier to read. Hope this helps!

  • Related