我已经编写了这段代码并且它完全有效,但是当我添加最后两行时,它继续说:“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)
答案 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)