如何从Python2.7上的列表中选择名称

2024-06-07 18:22:21 发布

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

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

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."

Tags: theto用户名称youraccountopenplc
2条回答

文件数据变量中的名称似乎用新行分隔,因此可以用'\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

相关问题 更多 >

    热门问题