首页 > 解决方案 > 为什么这会打印 Invalid Credentials 而不是 Logged In

问题描述

我正在学习 Python。我有这个代码

import csv

# Asking for user Inputs
user_name = input('Enter Username: ')
password = input('Enter Password: ')


def login(user_name, password):
    # Opening the CSV File
    with open('user_info.csv', 'r') as user_info_file:
        # Defining the file reader
        csv_reader = csv.reader(user_info_file)

        # Checking if the user credentials are correct
        for user in csv_reader:

            # Checking if the user_name and password are in the csv file
            if user[0] == user_name and user[1] == password:
                return True

    return False


authenticate = login(user_name, password)
if authenticate == True:
    print('Logged In')
else:
    print("Wrong Credentials")

当我输入与 csv 文件中相同的用户名和密码时,它正在打印Invalid Credentials而不是Logged in.

有人可以帮忙吗?

标签: pythonpython-3.x

解决方案


推荐阅读