然后代码工作在添加更多代码时停止工作

时间:2016-02-02 17:30:21

标签: list python-3.x integer undefined

我已经编写了这段代码并且它完全有效,但是当我添加最后两行时,它继续说:“Total”没有定义。没有最后两行代码完美,导致这种情况的原因是什么?

NumGiven=''
while not NumGiven.isnumeric():
    NumGiven=(input('Please enter a 7 or 8 digit number:'))
while len(NumGiven)<7 or len(NumGiven)>8:
    NumGiven=(input('Please enter a 7 or 8 digit number:'))
if len(NumGiven)==8:
    my_list=[int(i) for i in NumGiven]
    total=(sum([int(i) for i in NumGiven]))
ans = total / 10.0

if total % 10 == 0:
    print("Your GTIN8 code is correct.")
else:
    print("Your GTIN8 code is incorrect")
if len(NumGiven)==7:
    my_list=[int(i) for i in NumGiven]
    print(my_list)

1 个答案:

答案 0 :(得分:0)

Structure your code this way:

NumGiven=''
while not NumGiven.isnumeric():
    NumGiven=(input('Please enter a 7 or 8 digit number:'))
while len(NumGiven)<7 or len(NumGiven)>8:
    NumGiven=(input('Please enter a 7 or 8 digit number:'))
if len(NumGiven) == 8:
    my_list=[int(i) for i in NumGiven]
    total=(sum([int(i) for i in NumGiven]))
    ans = total / 10.0

    if total % 10 == 0:
        print("Your GTIN8 code is correct.")
    else:
        print("Your GTIN8 code is incorrect")
elif len(NumGiven) == 7:
    my_list=[int(i) for i in NumGiven]
    print(my_list)