随机数发生器和验证器

时间:2016-11-28 14:45:18

标签: python

这是我的gtin-8号码生成器的代码

barcode = []
inputChoice = 0
inputChoice = 1
checkNumber = 0
placeHolder = 0

def generate():
    generate = 0
    for i in str(Userinput):
        barcode.append(int(i))
    print(barcode)
    for i in range (0,6,2):
        generate += barcode[i] *3
        print('Generate is now ',generate)
    print(generate)

def checkNumber():
    for i in str(Userinput):
        checkNumber += int(i)
        placeHolder += int(i)
    checkNumber = round(checkNumber, -1)
    checkNumber = (checkNumber - placeHolder) % 10
    return(checkNumber)

def check():
    jk = 0

def main():
    inputChoice=0
    while inputChoice !=9 and inputChoice !=1 and inputChoice!=2:
        inputChoice=int(input("chose 1 to get GTIN,2 to check,9 to exit\n"))
        Userinput = (input("input the barcode \n"))
    if inputChoice==1 :
        if len(Userinput) !=7:
            print("make sure you inputted 7 inegers")
    if inputChoice == 2:
        if len(Userinput) !=8:
            print("Make sure you input 8 integers")
    else:
            generate(Userinput)
    return(Userinput)

Userinput = main()
generate()
checkNumber()

我已经创建了一个名为check number,generate和main的函数但是当我运行代码时它给了我这个错误:

  

TypeError:generate()接受0个位置参数但是给出了1个

我不确定为什么会这样,jk = 0就在那里,因为我还没有完成那个功能,我只是把它用作占位符。

2 个答案:

答案 0 :(得分:1)

添加* args后,

工作正常,而不是

UnboundLocalError: local variable 'checkNumber' referenced before assignment

所以在checkNumber()

的标志中添加默认args
def generate(*args): #<<<< edit
    generate = 0
    for i in str(Userinput):
        barcode.append(int(i))
    print(barcode)
    for i in range (0,6,2):
        generate += barcode[i] *3
        print('Generate is now ',generate)
    print(generate)

def checkNumber(checkNumber=0, placeHolder=0): #<<<< edit
    for i in str(Userinput):
        checkNumber += int(i)
        placeHolder += int(i)
    checkNumber = round(checkNumber, -1)
    checkNumber = (checkNumber - placeHolder) % 10
    return(checkNumber)

它的作品最终以1515155155151作为条形码:

chose 1 to get GTIN,2 to check,9 to exit
 2
input the barcode 
 1515155155151
Make sure you input 8 integers
[1, 5, 1, 5, 1, 5, 5, 1, 5, 5, 1, 5, 1]
Generate is now  3
Generate is now  6
Generate is now  9
9

答案 1 :(得分:0)

好的,你要求反馈,所以我们去......

第1点:当你不需要时,不要使用全局变量。提示:你可以编写没有单个全局的大型(10 + klocs)程序(谈论在执行过程中被修改或重新分配的全局变量 - 伪常量等等)。实际上,这开始于编写纯函数 - 带参数的函数,返回结果,并且同一组参数将始终产生相同的结果

第2点:尊重语言的常用编码约定(参见Python的pep8

第3点:学习语言的习语 - 在你的例子中,

  • 使用str.format()
  • 使用列表推导或生成器表达式而不是for循环,这是有意义的,
  • 一个main()函数真的一个main函数,并由if __name__ == "__main__":支票保护。

以下是修复这些点后代码的示例:

def generate(source):
    barcode = [int(c) for c in source]
    result = sum(barcode[i] * 3 for i in range(0, 6, 2))
    return result, barcode

def check_number(source):
    placeholder = checksum = sum(int(i) for i in source)
    checksum = round(checksum, -1)
    checksum = (checksum - placeholder) % 10
    return checksum

def check(what):
    jk = 0


def main():
    choice = 0
    while True:
        choice = input("chose 1 to get GTIN, 2 to check, 9 to exit\n").strip()
        try:
            choice = int(choice)
            if choice not in (1, 2, 9):
                raise ValueError
        except ValueError:
            print("Invalid value '{}'".format(choice))
            continue

        if choice == 9:
            return

        while True:
            source = input("input the barcode \n").strip()
            # source should contain only digits
            try:
                int(source)
            except ValueError:
                print("Make sure you entered only integers")
                continue

            if choice == 1 and  len(source) != 7:
                print("Make sure you entered 7 integers")
                continue
            elif choice == 2 and len(source) != 8:
                print("Make sure you entered 8 integers")
                continue
            else:
                # ok, valid input
                break

        if choice == 1:
            result, barcode = generate(source)
            print("barcode : {}.".format(barcode))
            print("result : {}".format(result))

        elif choice == 2:
            raise NotImplementedError("check is not yet implemented")

if __name__ == "__main__":
    main()