【问题标题】:Python: Generating and Validating barcodesPython:生成和验证条形码
【发布时间】:2017-03-05 17:43:17
【问题描述】:

我在计算课上设置了一项任务,我必须开发一个 python 程序来生成和验证 GTIN-8 条形码。然而,我的老师很穷,他一点头绪都没有,所以在课堂上寻求帮助并不是一个真正有效的选择。此外,在设置这个作业之前,我的班级几乎没有 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 的倍数中减去总和。最后,结果将是第八位(校验位),因此程序应该输出最终的条形码。

如果用户想要验证条形码,程序应该要求用户输入一个八位数的代码。然后它应该用三个和一个重复上述乘法过程。然后它应该将所有计算出来的数字相加,如果结果是 10 的倍数,则条形码是有效的,它应该向用户输出一条消息,说明 GTIN-8 是有效的。但是,如果不是 10 的倍数,则条码无效,应该打印错误。

无论如何,感谢您抽出宝贵时间阅读本文,我们将不胜感激。

【问题讨论】:

    标签: python


    【解决方案1】:

    除了 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 函数返回时,每次都会重新显示主菜单。而且您不会遇到递归调用自己的函数的问题。

    我还建议您不要只复制/粘贴您在网上找到的代码。你不会从中学到任何东西。真正确保您了解提供给您的答案以及它们是如何工作的。让某件事情发挥作用并不等同于理解它为什么发挥作用。

    【讨论】:

      【解决方案2】:

      这是您的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 个数字匹配。

      【讨论】:

        猜你喜欢
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 2015-11-30
        相关资源
        最近更新 更多