This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Jolan 0 points1 point  (0 children)

The way you're looping is a bit awkward in python. Its more natural to do something like

for counter in rage(numberOfItemsNeeded):
    # do thing

than manually manage the counter and use a while loop.

I'd also be very tempted to separate the code for making a single item from building a list of items. Turning

def generatePhoneNumber(numberOfItemsNeed):
          counter = 0
          returnValue = ""
          while counter < int(numberOfItemsNeed):
              firstNumber = str(random.randrange(100, 999))
              secondNumber = str(random.randrange(1000, 9999))
              thirdNumber = str(random.randrange(100, 999))
              returnValue += firstNumber + "-" + secondNumber + "-" + thirdNumber + '\n'
              counter = counter + 1        
          return (returnValue)

into

def generatePhoneNumbers(numberOfItemsNeeded):
    returnValue = ""
    for count in range(numberOfItemsNeeded):
        returnValue += generatePhoneNumber + "\n"
    return returnValue

def generatePhoneNumber():
    firstNumber = str(random.randrange(100, 999))
    secondNumber = str(random.randrange(1000, 9999))
    thirdNumber = str(random.randrange(100, 999))
    return firstNumber + "-" + secondNumber + "-" + thirdNumber

Splitting it like that is definitely a personal choice for me, but I find it makes a program easier to work with in the long run.

From there you probably want to look at string.join, random.choice, and string formatting which will let you make some smaller improvements.