我是python的新手,我想知道在特定的数据验证案例中构造和构建代码的最好方法是什么。我正在构建cmd行脚本,它接受3个参数并将它们保存到变量中。第三个参数是可选的。我应该如何处理可选参数。当我没有指定第三个参数时,提示我"IndexError: list index out of range“。
为什么用可选参数验证用户输入,最简单、最实用的原因是什么?
user_cat = sys.argv[1]
user_id = sys.argv[2]
user_guid = sys.argv[3]
def validate(user_cat, argv_cat_list, user_guid):
if len(user_cat) > 10 or user_cat not in argv_cat_list:
print("Error! Please specify a valid category (AddDevice, GetAccount, c, p, GetDevices, r ,s)")
sys.exit()
elif len(user_id) == 36 or user_id.startswith("SAM-") and len(user_guid) == False:
getInfo()
sys.exit()
elif len(user_cat) >= 10 and len(user_guid) == 36 and user_id.startswith("FRA-"):
print ("Test-hest!")
else:
print("Error! Please specify a valid input")
sys.exit()
https://stackoverflow.com/questions/50955730
复制相似问题