dimanche 20 juin 2021

How to handle adding a value to first line of txt file that has an existing value using python

Looking for help with file handling, please:

So, I'm new to Python, and I decided to go straight for it by writing a small program. Basically, the idea is to have a .txt file that keeps a running balance, on a single line, that is received from user input. However, I am struggling to understand how to write the user input into the value on the single line. With that said, this is what I have to offer for reference:

print("\n"""" Please choose an option: 
    Balance:
        1. View
        2. Update
    -------------    
    Savings: 
        3. View
        4. Update""""\n") 
number = int(input(" Enter a number: ")) 
if number == 1:
    view = open("current_balance.txt", "r")
    print("\n", "Balance: " + view.read())
elif number == 2: 
    new_entry = input("\n" + " Enter figure: ")  
    update_balance = open("current_balance.txt", "a+")          
    update_balance.write(new_entry)
    update_balance = open("current_balance.txt", "r")
    print(update_balance.read())
    update_balance.close()
elif number == 3:
    print("test")
elif number == 4: 
    print("test")

If it is not too much to ask, some general guidance would be great. Thanks!

Aucun commentaire:

Enregistrer un commentaire