Reputation: 15
I have been set a task in my Computing class that I have to develop a python program that generates and validates a GTIN-8 barcode. However, my teacher is very poor and he does not have a clue whatsoever so asking for help in class isn't really a valid option. In addition, prior to being set this assignment my class has had very little Python experience, and I am currently the very definition of a Python beginner. Anyway, here is all the code I have so far:
def mainmenu():
print("1. Generate a barcode")
print("2. Validate a barcode")
print("3. Quit") #prints out 3 options for the user to select
while True: #loops over and over again
try: #handles exceptions
selection=int(input("Enter choice: ")) #tells user to enter a choice
if selection==1:
generate() #calls the function
break #terminates the loop
elif selection==2:
validate()
break
elif selection==3:
break
else:
print("Invalid choice. Enter 1-3")
mainmenu()
except ValueError: #if user enters a string, it will loop back
print("Invalid choice. Enter 1-3")
exit
def generate():
print("You have chosen to generate a barcode")
D1 = int(input("Please enter the first digit"))
D2 = int(input("Please enter the second digit"))
D3 = int(input("Please enter the third digit"))
D4 = int(input("Please enter the fourth digit"))
D5 = int(input("Please enter the fifth digit"))
D6 = int(input("Please enter the sixth digit"))
D7 = int(input("Please enter the seventh digit"))
timestogether = int('D1') * 3
print (timestogether)
anykey=input("Enter anything to return to main menu")
mainmenu()
def validate():
print("You have chosen to validate a barcode")
anykey=input("Enter anything to return to main menu")
mainmenu()
# recalls the main menu
mainmenu()
So far, I have developed a menu which asks the user whether they want to generate or validate a barcode. However, I am not really sure what to do next. If the user wants to generate a barcode, the program must ask the user to input a seven digit number, then it must multiply the seven digits alternatively in order by 3 than 1, then it must add the outcomes together to get a sum, then it must subtract the sum from the nearest equal or higher multiple of 10. Finally, the result will be the eighth digit (the check digit) and therefore the program should then output the final barcode.
If the user wants to validate a barcode, the program should ask the user to enter a eight digit code. It should then repeat the multiplying process as above with the three and ones. It then should add all the calculated numbers up, and if the outcome is a multiple of 10, the barcode is valid and it should output a message to the user saying the GTIN-8 is valid. However, if it is not a multiple of 10, the barcode is invalid and it should print a error.
Anyway, thanks for taking the time to read this, and any help would be greatly appreciated.
Upvotes: 1
Views: 2241
Reputation: 3889
In addition to Claymore's answer, your main menu user inputs should be in the while loop:
def mainmenu():
while True: #loops over and over again
print("1. Generate a barcode")
print("2. Validate a barcode")
print("3. Quit") # prints out 3 options for the user to select
selection=int(input("Enter choice: ")) #tells user to enter a choice
if selection==1:
generate() #calls the function
elif selection==2:
validate()
elif selection==3:
break
else:
print("Invalid choice. Enter 1-3")
This way, when the generate and validate function returns, it will re-show the main menu every time. And you don't run into the issues of recursively calling your own functions.
I would also advise you not to just copy/paste code you find on online. You won't learn anything from that. Really make sure you understand the answers that are provided to you and how they work. Getting something to work is not the same as understanding why it works.
Upvotes: 2
Reputation: 695
Here is the solution to your generate
and validate
function:
def generate(arg=''):
GTIN = arg
if arg == '':
GTIN=(input("Enter a 7 digit GTIN number: "))
if(len(GTIN)==7):
G1=int(GTIN[0])
G2=int(GTIN[1])
G3=int(GTIN[2])
G4=int(GTIN[3])
G5=int(GTIN[4])
G6=int(GTIN[5])
G7=int(GTIN[6])
GTINT=int(G1*3+G2+G3*3+G4+G5*3+G6+G7*3)
roundup=round(GTINT, -1)
GTIN8 = int(roundup - GTINT) % 10
if arg == '':
print(arg)
print("Your full GTIN-8 code is: "+str(GTIN)+str(GTIN8))
return GTIN8
else:
print("Nope")
def validate():
GTIN=(input("Enter an 8 digit GTIN number: "))
GTIN8 = generate(GTIN[0:7])
if str(GTIN8) == GTIN[7]:
print("Your code is valid")
else:
print("Your code is invalid")
The validate
function simply uses the generate
function to create the 8th number and then checks if the generated 8th number matches the passed 8th number.
Upvotes: 0