Python List append()不追加用户输入
我有一个程序,我正在尝试获取用户输入并将其添加到数组中Python List append()不追加用户输入,python,login,Python,Login,我有一个程序,我正在尝试获取用户输入并将其添加到数组中 ids=['rex','test'] pwds=['rex','test'] def user(): 添加\用户名=原始\输入('用户名:') 添加\u password=raw\u输入('password:')) 附加(添加用户名) pwds.append(添加密码) 打印('\n添加:{}:{}'。格式(添加\用户名,添加\密码)) 用户() 我希望它能将输入添加到列表中,但却无法正常工作将项目添加到列表中不会在程序中添加实际的项目字
ids=['rex','test']
pwds=['rex','test']
def user():
添加\用户名=原始\输入('用户名:')
添加\u password=raw\u输入('password:'))
附加(添加用户名)
pwds.append(添加密码)
打印('\n添加:{}:{}'。格式(添加\用户名,添加\密码))
用户()
我希望它能将输入添加到列表中,但却无法正常工作将项目添加到列表中不会在程序中添加实际的项目字符串 源文件 如果需要,可以打开一个新文件并将列表写入其中 例如:
ids = ['rex', 'test']
pwds = ['rex', 'test']
def user():
adding_username = raw_input('username: ')
adding_password = raw_input('password: ')
ids.append(adding_username)
pwds.append(adding_password)
# print the ids list
print(ids)
# print the pwds list
print(pwds)
# write the lists to a file "myfile.txt"
with open("myfile.txt", "w") as myfile:
# use the 'str' function on an object to turn it to string
# or in other words turn it to "text"
myfile.write(str(ids))
myfile.write("\n") # start new line in file
myfile.write(str(pwds))
print('\nadded: {}:{}'.format(adding_username, adding_password))
user()
现在,如果您在同一目录中打开名为“myfile.txt”的文件,您将发现其中的列表肯定会添加到列表中。你说的“但不悲伤地工作”是什么意思?你是怎么查名单的?您的
打印
只是打印输入,而不是列表。我正在重新打开文件,这就是我检查的方式,您如何写入文件?显示a。我认为append会将字符串添加到列表中,并自己编写,您建议我如何执行此操作?否。添加到列表并不意味着在程序运行之间保存该列表<代码>ID仅在程序打开时存在。您需要手动将列表保存到文件中,并在以后需要使用时从磁盘中读取它。可能是一个很好的介绍。