我的程序询问用户是否有帐户。如果用户没有帐户,则程序将为用户创建一个新帐户并将其详细信息存储在csv文件中。这部分工作得很好。但如果用户再次运行该程序(在创建帐户后)并尝试使用相同的详细信息(用户名、密码)登录,则无法正常工作。如何将登录信息与csv数据进行比较并授予访问权限?在
##Two Subjects----> Computer Science and History
import csv
def main():
userAccount()
def userAccount():
print('-------------------------------')
userAccount = input('Do you already have an account: ')
if userAccount == 'No' or userAccount== 'N' or userAccount == 'no' or userAccount == 'n':
userSName = input('Name : ')
userSAge = input('Age : ')
userSYgroup = input('Year Group : ')
userGname = userSName[:3] + userSAge
print('Your username is : ',userGname.lower())
userSword = input('Password: ')
detail1 = [userSName,userSAge,userSYgroup,userGname,userSword]
with open('userData.csv','w',newline = '') as csvfile:
w = csv.writer(csvfile,delimiter = ',')
w.writerow(detail1)
elif userAccount == 'Yes' or userAccount == 'Y' or userAccount == 'yes' or userAccount == 'y':
userLName = input('Username: ')
passLWord = input('Password: ')
f = open('userData.csv','r',newline = '')
for i in f:
if userLName == f[0,3] and passLWord == f[0,4]:
print('Log In Successfull')
else:
print('Incorrect Username or Password')
main()
main()
这是一个完全工作的版本,稍作改动。将其添加到原始代码中。虽然它可能会破坏您当前的代码,因为我更改了几个变量的名称。相应地重命名它们。在
相关问题 更多 >
编程相关推荐