我已经在我的电脑类被设定的任务,我要开发生成和验证一个GTIN-8条形码的Python程序。然而,我的老师很穷,他也没有任何线索,所以在课堂上寻求帮助并不是一个真正有效的选择。另外,在设置这个赋值之前,我的类没有Python经验,而且我目前是Python初学者的定义。总之,这里是我到目前为止的代码:的Python:生成和验证条形码
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()
到目前为止,我已经开发,询问他们是否要产生或验证条形码用户的菜单。但是,我不确定接下来要做什么。如果用户想要生成条形码,程序必须要求用户输入一个七位号码,那么它必须由3或者乘以七个数字级别比1,那么就必须添加的结果一起得到的总和,然后它必须从最接近的相等或更高的10倍中减去总和。最后,结果将是第8位数字(校验位),因此程序应该输出最终的条形码。
如果用户要验证条形码,程序应该要求用户输入一个八位代码。然后它应该重复与上述三个和一个的乘法过程。那么它应该增加全部计算出的数字了,如果结果是10的倍数,条形码是有效的,它应该输出信息给用户称GTIN-8是有效的。但是,如果它不是10的倍数,则条形码无效,应该打印出错误。
无论如何,感谢您抽出时间来阅读这一点,任何帮助将不胜感激。