2017-03-05 72 views
1

我已经在我的电脑类被设定的任务,我要开发生成和验证一个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的倍数,则条形码无效,应该打印出错误。

无论如何,感谢您抽出时间来阅读这一点,任何帮助将不胜感激。

回答

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号,然后检查。

2

除了双刃大砍刀的答案,你的主菜单中用户输入应该是在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") 

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

我也劝你不要把刚才复制/粘贴代码,你找到关于网上。你不会从中学到任何东西。确保你了解提供给你的答案以及他们的工作方式。让事情工作与理解为什么工作不一样。