我正在寻找一个有趣的任务,我在搜索时发现了进行强大的验证,下面只是我的验证代码。
Pan = “1234567890-=[]{}%^*+_\|~<>€$¥•.,?!/()£&@“ # this is my list that I’m going to check an input against
Valid = False
while Valid ==False:
Valid = True
Forename=input(“Please enter name”)
For i in range (0,len(pan)):
if input[i] in pan == False:
Valid == False
我在行
中遇到语法错误if input[i] in pan == False:
消息是
TypeError:'builtin_function_or_method'对象不可订阅
提前抱歉有点noo
答案 0 :(得分:1)
一些注意事项:
input[i]
Pan
中not
是否应该Valid==False
Valid=False
应为For
,因为您正在设置变量pan = “1234567890-=[]{}%^*+_\|~<>€$¥•.,?!/()£&@“ # this is my list that I’m going to check an input against
valid = False
while valid == False:
valid = True
forename = input("Please enter name")
for i in range (0,len(pan)):
if input[i] not in pan:
valid = False
应以小写字母尝试使用以下内容,我稍微清理了变量
name = input("Please enter a name")
valid = True
for char in pan:
if char in name:
valid = False
break
这是一个更简洁的版本
function checkResult(req, callback){
let result = true;
Reservation.find({result_date: req.body.res_date}, function (err,doc) {
if (err) {console.log(err);}
else if (reservations) {
result = false;
}
callback(result);
})
}
答案 1 :(得分:0)
您发布的代码中存在多个问题。首先,您要求输入名称由 Pan 中的字符组成 - 名称通常是字母,因此我担心您的逻辑是相反的。但是......
以下是这些更新后的代码:
pan = “1234567890-=[]{}%^*+_\|~<>€$¥•.,?!/()£&@“ # valid characters
forename = input(“Please enter name”)
valid = False
while not valid:
valid = True # input is good until we get a bad value
for i in range (0, len(forename)):
if forename[i] not in pan:
valid = False
break
接下来,请注意,您实际上并不需要每个字符的索引;你只需要角色。 user6731765的答案显示了此更新。
对于高级缩短,请了解所有和任何功能; all 可以在这里轻松检查整个输入字符串:
forename = "INVALID NAME"
# Are all the name's characters found in pan?
while not all(char in pan for char in forename):
forename = input(“Please enter name”)