我将如何为特定用户打印fav_genre字段



所以下面的代码将变量用户名传递等保存到一个文本文件中,并用冒号分隔每个字段。 假设我想打印出特定用户最喜欢的类型,这怎么可能,因为我到目前为止的尝试都失败了。 顺便说一句,这一切都在 Python 中。 我编辑了添加我的尝试,但它没有醒来。任何想法??

usrFile_write = open(textfile, 'a')
usrFile_write.write(username + ' : ' + password + ' : ' + name + ' : ' + dob + ' : ' + fav_artist + ' : ' + fav_genre + ' : ' + 'n')
print('New Account Created!')
print()
usrFile_write.close()
Menu()

我的尝试:

textfile = 'user_DB.txt'
username = 'dj'
def getUsersFavouriteGenre():
    with open(textfile, 'r') as textIn:
        for line in textIn:
            information = line.split(' : ')
            if information[0] == username:
                return information[5]
    return 'Failed to find user.'
getUsersFavouriteGenre()

你的代码看起来不错。我假设问题是您的程序没有输出任何内容,在这种情况下,我看到的唯一问题是将最后一行getUsersFavouriteGenre()更改为print(getUsersFavouriteGenre())

如果您遇到特定错误或不需要的输出,您可以告诉我们,我们可以更具体地为您提供帮助。

最新更新