1

我在 python 中有一个 cgi 脚本,它以表单形式进行搜索操作。表单具有名字、姓氏、年龄和性别。

现在,如果用户输入年龄并单击男性,它应该在输入的字段中显示男性候选人而不是具有指定年龄的女性候选人。

我应该写什么条件

假设有像这样的数据

john duck 25 male
sashi tharoor 34 male
jacque kalis 25 male
amanda seyfried 25 female

如果用户输入年龄为“25”并在表单中选择“男性”,它应该只显示雅克和约翰的详细信息

        if Fname.lower() == temp[0]:
            found = True

        if Lname.lower() == temp[1]:
            found = True

        #if Age and Age == temp[2]:
            #found = True

        #if Gender and Gender == temp[3] :
            #found = True

        if Age == temp[2] and Gender == temp[3]:
            found = True

如果我这样写,而不是在表单上单击性别按钮。它什么也不会显示

4

3 回答 3

1
with open('data.txt') as f:
  line = f.read().split('\n')
  sex = 'male'
  age = '25'
  for l in line:
    tmp = l.split(' ')
    if sex == tmp[3] and age == tmp[2]:
      print tmp[0], tmp[1]
于 2013-10-31T09:52:27.913 回答
0
if Fname == Fsearch and Lname == Lsearch and Age == Asearch and Gender == Gsearch:
    found = True 
于 2013-10-31T11:50:47.920 回答
0

你可以用'keyword' in 'text'

例如:

line = 'john duck 25 male'

'john' in line
> True

'john' and 'duck' in line
> True

'john' and 'female' in line
> False

要检查所有输入的值是否有效,您可以执行类似的操作

#genderEntered should be True if something was entered and False otherwise
if genderEntered == True:
    genderCorrect = gender in line

#do this for all fields

#all correct?
if genderCorrect and nameCorrect:
    #all values are correct
else:
    #at least one was not correct
于 2013-10-31T09:47:16.227 回答