我有以下文件(银行详情.txt)你知道吗
customer1,1
customer2,2
customer3,3
customer4,4
customer5,5
以下代码旨在: 1要求用户输入customerid和帐号(例如customer1和1),如果匹配文件详细信息,则打印“access granted”,否则打印“denied”。你知道吗
代码如下。(注意?我不知道如何进行)
def read_from_file_csv_method2():
accessgranted=False
while accessgranted==False:
with open("bankdetails.txt","r") as f:
reader=csv.reader(f)
for row in reader:
for field in row:
username=input("uesrname:")
accountno=input("account no:")
if username==field and accountno==?:
accessgranted=True
break
else:
accessgranted=False
if accessgranted==True:
print("Access Granted")
else:
print("Sorry, wrong credentials")
请回答以下问题
更新: 我知道字典是最合适的,但是我想用这些结构来解决这个问题。你知道吗
例如,下面的代码只适用于第一次迭代(customer1和1)…不适用于其他任何东西。你知道吗
if username==row[0] and accountno==row[1]:
在下面的代码中使用了上面的代码…但是仍然不能正常工作
def read_from_file_csv_method2():
accessgranted=False
while accessgranted==False:
with open("bankdetails.txt","r") as f:
reader=csv.reader(f)
for row in reader:
username=input("uesrname:")
accountno=input("account no:")
if username==row[0] and accountno==row[1]:
accessgranted=True
break
else:
accessgranted=False
if accessgranted==True:
print("Access Granted")
else:
print("Sorry, wrong credentials")
你可以试试这个:
更简单的方法包括and函数和for循环:
没有函数:
相关问题 更多 >
编程相关推荐