当我询问被提名者档案中的某部电影是否与我的用户输入的电影相同时,我的代码失败了:
#!python
import csv
with open('nominees.csv', newline='') as f:
winningtitle=input("Winning title: ")
for line in csv.DictReader(f):
if line['title']==winningtitle:
print('Congratulations:', line['director(s)'])
但是,当我询问电影标题的长度是否与用户输入的长度相同时,它可以工作:
#!python
import csv
with open('nominees.csv', newline='') as f:
winningtitle=input("Winning title: ")
for line in csv.DictReader(f):
if len(line['title'])==len(winningtitle):
print('Congratulations:', line['director(s)'])
显然,Code#2是作弊。代码#1有什么问题?