我有一个包含一些信息的CSV,并且代码将遍历CSV中的每一行,并且如果输入的用户名与该行中的值匹配,它将允许用户登录。
但是,我不确定在细节不正确的情况下如何允许我的程序说出来。每次迭代后(而不是在CSV末尾)都会打印出“未找到”。
我怎样才能做到,一旦它在for循环的末尾,它说找不到详细信息?
谢谢。
username = str(input("Enter your username: "))
password = str(input("Enter your password: "))
file = open("details.csv","r")
print('Details opened')
contents = csv.reader(file)
print('reader established')
for row in contents:
print('begin loop')
if username == row[4]:
print("Username found")
if password == row[3]:
print("Password found")
main()
else:
print("not found")
一个简单的解决方案是添加变量is_found
作为示例:
is_found = False
for row in contents:
print('begin loop')
if username == row[4]:
print("Username found")
if password == row[3]:
print("Password found")
main()
is_found = True
if not is_found:
print("not found")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句