联合在一起时验证用户输入?

时间:2016-09-11 12:15:50

标签: python-3.x

在我的代码中,它应该验证用户输入是一个数字,8位数,并在我的文本文件中。在我将验证分开之前,它会接受我显然不想要的任何8位数字,所以当我合并它们时,我会继续获得语法错误。 (第5行)

GTIN=''
items=[]
while len(items) < int(itemsneeded):
            GTIN=(input('Please enter all GTIN-8 for all items'))
            if GTIN.isdigit() and len(GTIN)==8 and in open('read_it.txt').read():
                Num0=int(GTIN[0])*3
                Num1=int(GTIN[1])
                Num2=int(GTIN[2])*3
                Num3=int(GTIN[3])
                Num4=int(GTIN[4])*3
                Num5=int(GTIN[5])
                Num6=int(GTIN[6])*3
                Num7=int(GTIN[7])
                total2=(Num0+Num1+Num2+Num3+Num4+Num5+Num6+Num7)
                if total2 % 10 == 0:
                    print(GTIN)
                items.append(GTIN)
            else:
                print("Product Not Found")
print(items)

1 个答案:

答案 0 :(得分:0)

而不是

if GTIN.isdigit() and len(GTIN)==8 and in open('read_it.txt').read():

使用

if GTIN.isdigit() and len(GTIN)==8 and GTIN in open('read_it.txt').read():

你必须把

...GTIN in open('read_it.txt').read()