@ADoctor/

Barcodes Task 1

Python

Previous name: LegitimateVillainousThings

fork
loading
Files
  • main.py
main.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
def calcNum():
	while True:
		print("")
		try:
			num = int(input("Please enter the number you wish to calculate the check digit for: "))
			if len(str(num)) == 7:
				break
			else:
				print("Sorry, that number's not the right length. It must be exactly 7 digits long.")
		except ValueError:
			print("Sorry, but you need to input a number. Please try again.")
	total = 0
	for i in range(0,7):
		if i % 2 == 1:
			total += int(str(num)[i])
		else:
			total += 3*int(str(num)[i])
	counter = 0
	while counter < total:
		counter += 10
	checkdigit = counter - total
	print("The check digit is",checkdigit)
	num = int(str(num)+str(checkdigit))
	print("The full barcode is",num)
	print("")

def checkNum():
	while True:
		print("")
		try:
			num = int(input("Please enter the barcode you wish to validate: "))
			if len(str(num)) == 8:
				break
			else:
				print("Sorry, that barcode's not the right length. It must be exactly 8 digits long.")
		except ValueError:
			print("Sorry, but you need to input a number. Please try again.")
	total = 0
	for i in range(0,8):
		if i % 2 == 1:
			total += int(str(num)[i])
		else:
			total += 3*int(str(num)[i])
	if total % 10 == 0:
		print("Valid barcode!")
	else:
		print("Invalid barcode...")

def main():
	while True:
		try:
			option = int(input("""Welcome to the GTIN-8 Barcode Checker!

	Which option would you like to choose?

	1. Calculate Check Digit
	2. Check Barcode Validity
	3. Quit

	Choose the relevant number: """))
			if option == 1:
				calcNum()
			elif option == 2:
				checkNum()
			elif option == 3:
				break
			else:
				print("Sorry, that's not a valid option. Please enter the number of the option you wish to choose.")
		except ValueError:
			print("Sorry, that's not a valid option. Please enter the number of the option you wish to choose.")

main()