Python:生成和验证条形码

时间:2017-03-05 17:43:17

标签: python

我已经在我的计算类中设置了一个任务,我必须开发一个生成并验证GTIN-8条形码的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的倍数,则条形码无效,应该打印错误。

无论如何,感谢您花时间阅读本文,并且非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

除了Claymore的回答,您的主菜单用户输入应该在while循环中:

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")

这样,当generate和validate函数返回时,它每次都会重新显示主菜单。而且你不会遇到以递归方式调用自己的函数的问题。

我还建议您不要复制/粘贴您在网上找到的代码。你不会从中学到任何东西。确保您了解提供给您的答案以及它们的工作原理。获得某些东西与理解其工作原理并不相同。

答案 1 :(得分:0)

以下是generatevalidate功能的解决方案:

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")

validate函数只使用generate函数创建第8个数字,然后检查生成的第8个数字是否与传递的第8个数字匹配。