0
이 코드는 html의 데이터를 가져 와서 afile에서 검색합니다. 검색이 발견되면 웹 브라우저에서 인쇄합니다. 이제 나는 found = False가 b.append() 행 다음에 쓰여진 이유에 관한 질의를했습니다. 우리가 쓰기 해달라고하면 b'coz 그것은 단순히 각 루프 패스의 끝에서 False
에 found
변수를 갱신 할 수 있습니다 웹 브라우저found = 거짓. 코드에 왜 쓰여 있습니까?
#!/usr/bin/python
import cgi
def get_search():
found = False
form = cgi.FieldStorage()
Fname = form.getvalue('firstname', '')
Lname = form.getvalue('lastname', '')
Age = form.getvalue('age', 0)
Gender = form.getvalue('gender', '')
f = open("/tmp/abcd.txt","r")
b = []
for line in f:
temp = line.split()
Fsearch = temp[0]
Lsearch = temp[1]
Asearch = temp[2]
Gsearch = temp[3]
if Fname and Fname.lower() in Fsearch.lower():
found = True
if Lname and Lname.lower() in Lsearch.lower():
found = True
if Age and Age == Asearch:
found = True
if Gender and Gender in Gsearch:
found = True
if found:
b.append(line)
found = False
f.close()
print "Content-type:text/html\n"
if len(b) == 0:
print "No records found"
else:
for each in b:
print each
get_search()
아무런 결과가 발견되지 않더라도, 발견 된 것이 없으면 항상 발견됩니다. . 그리고 어떤 값도 찾지 못하면, 첫 번째 루프에서 발견 된 키워드가 발견되지 않으면 found가 정의되지 않았으므로 프로그램에 오류가 발생할 수 있습니다 – fyr0049