def get_pseudonym(nhs_number): global GENERATE_NEW pseudo = LOOKUP.get(nhs_number) if pseudo is not None: return pseudo if GENERATE_NEW is not True: print("I have encountered a new NHS number (%d) with no pseudonym.\n" "Should I generate new ones for any new NHS numbers I find " "from now on?" % nhs_number) response = raw_input("type y or n:") if response == 'y': GENERATE_NEW = True else: print("In that case, I will exit now.") exit() while True: digits = [] s = '' tot = 0 for i in range(9): if i == 0: digit = random.randint(1, 9) else: digit = random.randint(0, 9) digits.append(digit) s += str(digit) tot += digit * (10 - i) # (10 - i) is the weighting factor checksum = 11 - (tot % 11) if checksum == 11: checksum = 0 if checksum != 10: # 10 is an invalid nhs number s += str(checksum) pseudo = int(s) LOOKUP[nhs_number] = pseudo return pseudo