我附加到.txt文件的元组将不会保存到文件

时间:2017-11-14 18:55:44

标签: python file text

所以我正在制作一个保存存款,取款和余额记录的atm程序。这是我的存款功能

def deposite(bal):
    d=input("How much would you like to deposite? ")
    newbal = float(bal) + float(d)
    write = "deposit ", str(d), str(newbal)
    print(write)
    f1 = open('atmrecord', 'a')
    f1.write(str(write) + '\n')
    print(f1.closed)
    f1.close()
    print("Your new balance is", newbal)
    return newbal

'写'是我知道的一个元组,但我不能为我的生活找出为什么它不会保存到.txt文件。在我的代码中,我能够读取文件以获得平衡,我认为我的写命令正确,但它不会保存。代码运行良好,没有任何错误,它似乎在它完成时关闭文件,但是当我在运行后打开我的.txt时,从来没有额外的数据行。任何想法/提示将不胜感激。 还要写'' =('存款',' 1',' 10001.0')如果d = 1,例如

1 个答案:

答案 0 :(得分:0)

您不想在str上致电write,因为它会返回('deposit ', '10', '20.0')之类的内容。只需:

def deposite(bal):
    d = input("How much would you like to deposite? ")
    newbal = float(bal) + float(d)
    write = "deposit ", str(d), str(newbal)
    print(write)

    with open('atmrecord', 'a') as f:
        f.write(" ".join(write) + '\n')

    print("Your new balance is", newbal)
    return newbal