dimanche 22 septembre 2019

creating a random phone number generator, want to find a way to decrease this loop in loop in loop chunk of code existing in a class

Description of program:

1.makes unique random phone numbers based on how many you want it to i mean: if you pass 100 it makes 100 phone numbers.

2.creates text files based on the range you pass to it, i mean: if you need 100 text files containing 100 unique phone numbers either unique comparing to each number within or the other to be made forthcoming text files.

meanwhile it creates phone numbers sorts the phone numbers like below if it makes sense:

This format to expect in the text files :

    • 1909911304
    • 1987237347

........... and so on.............

This is the method responsible to do so:

(Note: I use make_numbers method as construction of operation, Actually num_doc_amount should be used.)

    def make_numbers(self):
        """dont use this module:this module supports num_doc_amount method"""
        # sorry for this amount of loops it was inevitable to make the code work
        for number_of_files in range(self.amount_numbs):
            # this loop maintains the pi_digits.txt making(txt)
            number_of_files += 1
            if number_of_files == self.amount_files:
                sys.exit()
            for phone_numbers in range(self.amount_numbs):
                # This loop maintains the amount of phone numbers in each pi_digits.txt
                file = open(f"{self.directory}\\{number_of_files}.{self.format}", 'w')
                for numbers in range(self.amount_numbs):
                    # This loop is parallel to the previous one and
                    # writes that each number is which one from the
                    # whole amount of numbers
                    file.write(f"{numbers + 1}. - {self.first_fourz}{choice(nums)}"
                               f"{choice(nums)}{choice(nums)}{choice(nums)}"
                               f"{choice(nums)}{choice(nums)}{choice(nums)}\n")

    def num_doc_amount(self):
        """first make an instance and then you can use this method."""
        os.mkdir(f"{self.directory}")  # makes the folder
        for num_of_txt_files in range(self.amount_files):
            # This loop is for number of text files.
            num_of_txt_files += 1
            self.make_numbers()

Note That:

1.The only problem i have is with those parallel loops going with each other, i don't know if i can make the code simplified.(please let me know if i can be simplified.)

2.The code works and has no error.

if there is any way to make this code simplified please help me.Thank you.




Aucun commentaire:

Enregistrer un commentaire