-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZipcodeProj.py
137 lines (111 loc) · 4.04 KB
/
ZipcodeProj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#Author: Zach Naymik
#Date: February 20, 2022
#Description: This program takes a zipcode entered by a user and transforms into a bar code value,
# this is what happens in a real post office, and we are trying to replicate that here.
#Function that gets the code for an individual digit
def getDigitCode(digit):
digit_code = ""
if (digit == '1'):
digit_code = ":::||"
elif (digit == '2'):
digit_code = "::|:|"
elif (digit == '3'):
digit_code = "::||:"
elif (digit == '4'):
digit_code = ":|::|"
elif (digit == '5'):
digit_code = ":||::"
elif (digit == '6'):
digit_code = "|:::|"
elif (digit == '7'):
digit_code = "|:::|"
elif (digit == '8'):
digit_code = "|::|:"
elif (digit == '9'):
digit_code = "|:|::"
elif (digit == '0'):
digit_code = "||:::"
return digit_code
#Function that checks to see if the given zipcode is all digits or not
def isDigits(zip):
counter = 0
t_or_f = True
for i in zip:
if (i.isdigit() == False):
counter += 1
if (counter > 0):
return False
else:
return True
#Function that returns the check value for the end of the zipcode barcode
def getCheckDigitValue(sum):
return (10 - (sum % 10))
#Function that gets the sum of all the numbers in the zip in order to get the check digit
def getSumNumbers(zip):
for i in zip:
total_value = 0
ascii_value = ord(i)
actual_num = ascii_value - 48
total_value += actual_num
return total_value
#Function that gets the barcode for the entire zipcode
def getWholeZip(zip):
full_code = "|"
char_list = list(zip)
for i in char_list:
digit_code = getDigitCode(i)
full_code += digit_code + " "
check_digit_int = getCheckDigitValue(getSumNumbers(zip))
if (check_digit_int == 1):
check_digit_char = '1'
elif (check_digit_int == 2):
check_digit_char = '2'
elif (check_digit_int == 3):
check_digit_char = '3'
elif (check_digit_int == 4):
check_digit_char = '4'
elif (check_digit_int == 5):
check_digit_char = '5'
elif (check_digit_int == 6):
check_digit_char = '6'
elif (check_digit_int == 7):
check_digit_char = '7'
elif (check_digit_int == 8):
check_digit_char = '8'
elif (check_digit_int == 9):
check_digit_char = '9'
elif (check_digit_int == 0):
check_digit_char = '0'
check_digit_code = getDigitCode(check_digit_char)
full_code += check_digit_code + "|"
return full_code
#Initialize the more codes string in order to enter the while loop
more_codes = ' '
#While loop that continues if the user wished to enter more zipcodes
while more_codes != 'n':
#Initialize sentinel value in order to enter the while loop
sentinel_value = True
#While loop used to verify that the input follows our conditions
while sentinel_value == True:
user_zip = input("Enter a zip code: ")
if (len(user_zip) < 5 or len(user_zip) > 5):
print("Error: Zip code must be 5 digits")
sentinel_value = True
elif (isDigits(user_zip) == False):
print("Error: Invalid characters in zip code")
sentinel_value = True
else:
sentinel_value = False
#Calls the getWholeZip function and stores the whole zipcode barcode into the whole_zip variable
whole_zip = getWholeZip(user_zip)
#Prints out the zipcode barcode to the user
print(f"Code: {whole_zip}")
#Resets the more_codes variable to nothing to allow the while loop to be entered
more_codes = ' '
#While loop that asks the user if they wish to enter more zipcodes and verifies they entered a valid choice
while (more_codes != 'y' and more_codes != 'n'):
more_codes = input("More codes (y/n)? ")
if (more_codes != 'y' and more_codes != 'n'):
print("Error: Invalid selection")
#Prints exit prompt if user decides to not enter any more codes
print("Goodbye!")