如何检查python中输入的内容是否与csv-fi中的内容相同

2024-09-30 08:25:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个程序,看看输入python程序的用户名和密码是否与存储在csv文件中的用户名和密码相同。你知道吗

logorsign = input("""Choose one:
1. Log in
2. Sign up
3. Quit
""")
print("")

if logorsign == "1":           
    Netflix_CSV = csv.reader(open("Netflix Task.csv", "rt"))       
    first_Line = next(Netflix_CSV)
    checkuser = input("Enter Username: ")
    print("")
    checkpasw = input("Enter Password: ")
    for row in Netflix_CSV:
        if row[0]  == checkuser:
            print(watched_films)

以上是目前为止的代码。你知道吗

请帮帮我

提前谢谢


Tags: 文件csvin程序密码inputif用户名
1条回答
网友
1楼 · 发布于 2024-09-30 08:25:43

这里使用的最佳数据结构是字典:

user_passwords = dict()
user_films = dict()
user_logged_in = False

given_username = input('Enter Username: ')
print('')
given_password = input('Enter Password: ')
print('')

with open('Netflix Task.csv', 'r') as fh:
    reader = csv.reader(fh)
    reader.next()   # Skip the header line
    for line in reader:
        username, password, watched_films = line
        user_passwords[username] = password
        user_films[username] = watched_films

if user_passwords.get(given_username, False) == given_password:
    print('login successful')
    user_logged_in = True
else:
    print('Bad username/password')

然后,要访问用户的影片:

users_films = user_films[username]

相关问题 更多 >

    热门问题