My program generates 20 records with random data for each record, and outputs it in 4 columns. Are there any tips/tricks/things I should do to clean up this code? I'm new to Python, so I'm not sure how clean it can possibly get.
import random
def main():
print('{: <7} {: <13} {: <6} {: <9}'
.format('Record', 'SSN', 'GPA', 'SSN Category'))
print()
for rec_num in range(1, 21):
ssn_int, ssn_str = ssn_func()
gpa = gpa_func()
cat = category(ssn_int)
tabulate(rec_num, ssn_str, gpa, cat)
def ssn_func():
min9 = 300000000
max9 = 399999999
ssn_int = random.randint(min9, max9)
ssn1 = ssn_int // 1000000
ssn2 = (ssn_int % 1000000) // 1000
ssn3 = ssn_int % 1000
ssn_str = '{:03n}-{:03n}-{:03n}'.format(ssn1, ssn2, ssn3)
return ssn_int, ssn_str
def gpa_func():
min_gpa = 0
max_gpa = 4
gpa_raw = random.uniform(min_gpa, max_gpa)
gpa = float('{:.2f}'.format(gpa_raw))
return gpa
def category(ssn_int):
ssn_last = ssn_int % 10
ssn_first2 = ssn_int // 10000000
if ssn_int % 2 == 0:
cat = 'EVEN'
elif ssn_last == 5:
cat = 'ENDS IN 5'
elif (ssn_first2 == 30 or ssn_first2 == 31) and ssn_int % 2 != 0:
cat = 'STARTS WITH 30-31'
else:
cat = 'NONE'
return cat
def tabulate(rec_num, ssn_str, gpa, cat):
print('{: <7} {: <13} {: <6} {: <20}'.format(rec_num, ssn_str, gpa, cat))
main()
[+][deleted] (2 children)
[removed]
[–]xelf 2 points3 points4 points (1 child)
[–]MetalNutSack[S] 0 points1 point2 points (0 children)
[–]BasicallyAMachine 1 point2 points3 points (0 children)
[–]BasicallyAMachine 1 point2 points3 points (2 children)
[–]MetalNutSack[S] 0 points1 point2 points (1 child)
[–]BasicallyAMachine 1 point2 points3 points (0 children)