我想让用户输入一个8位数的条形码。如果代码长度不是8位,则会输出错误消息。如果长度为8位,则会引发错误。
def get_user_input():
global total_price
""" get input from user """
while len(str(GTIN))!=8:
try:
GTIN = int(input("input your gtin-8 number:"))
if len(str(GTIN))!=8:
print("make sure the length of the barcode is 8")
else:
print("make sure you enter a valid number")
return GTIN
答案 0 :(得分:0)
这里实际上有几个错误:
您的新代码:
def get_user_input():
global total_price
""" get input from user """
GTIN = ""
while True:
try:
GTIN = int(input("input your gtin-8 number:"))
if len(str(GTIN)) == 8:
break
else:
print("make sure the length of the barcode is 8")
except:
pass
return GTIN
get_user_input()