我如何从 python 2.7 上的列表中 select 一个名字

How do I select a name from a list on python 2.7

我的银行 ATM 学校项目有更多帮助。问题是我创建了一个程序,允许用户在其他地方的存档中输入名称,但我无法 select 从该存档中输入名称以供再次使用。这是我的代码。

f1 =open('C:\NameAgeFile', 'r')
filedata = f1.read()
f1.close()

print filedata

这会打印 shell 上所有注册用户的列表,但是我怎样才能做到这一点,如果用户想重新登录,请在 "Michael" 上说列表,然后检索该用户的银行余额?

balance_user = 500
f1 =open('N:\userFile', 'r')
filedata = f1.read()
f1.close()

print filedata
print "Welcome to the Banking Bank PLC public ATM service"
print "If you do have an account allready, select your name from the list of registered users."
user = raw_input("Please enter your ATM account name and insert your Banking Card. If you do not have an ATM account, please insert your Banking Card and enter New_User.")
if user == "New_User":
    print "Weclome to the Banking Bank PLC ATM service."
    user = raw_input ("Please enter an ATM account name that you wish to use to log onto the Banking Bank ATMs")

    f1 =open('N:\userFile', 'a+')
    print >> f1,user


print "Welcome", user, "to the Banking Bank PLC ATM service."
print "You have been given £100 for signing up. Please log out and back on to bank."

Names in your filedata 变量似乎用换行符分隔,所以你可以用'\n'字符和把它放到一个列表中。

从那里您可以检查用户输入的名称是否在列表中。

name_list = filedata.split('\n')

if user in name_list:
    #your login code

一种更符合 Python 风格的文件读取方式是使用 with 作用域并使用 readlines() 方法:

path = 'N:\users_file'
username = 'some_username'

# Open the file
with open(path, 'r') as file:
    # Iterate over the lines in the file
    for user in file.readlines():
        if user == username:
            # Do something