0

It is saying TypeError on line12 of createAccount(). I passed the balance variable which is float type and thought that the string formatting would convert it into float with 2 decimal places. Not sure why I am getting TypeError

from datetime import datetime

def menu():
    selection = int(input("Enter 1 to create and account:\nEnter 2 to deposit:\nEnter 3 to withdraw\nEnter 4 to quit: "))
    return selection

def createAccount():
    name = input("Enter customer name: ")
    filename = '{}.txt'.format(name)
    balance = float(input("Enter the initial deposit amount: "))
    with open(filename,'w') as balance:
        print('The balance is ${:.2f}'.format(balance))
        balance.write('{:.2f}\n'.format(balance))
    with open('log.txt','a') as log:
        timestamp = datetime.now().strftime("%d.%b %Y %H:%M:%S")
        log.write('{},customer:{}\n'.format(timestamp,name))
        log.write('transactiontype:deposit:amt:{.2f\n}'.format(balance))

def deposit():
    name = input("Enter customer name: ")
    filename = '{}.txt'.format(name)
    deposit = float(input("Enter the deposit amount: "))
    with open(filename,'r') as balance:
        current_balance = balance.readline()
        number = current_balance.rstrip()
        new_balance = float(number) + deposit
        print('Your new balance is ${:.2f}'.format(new_balance))
    with open(filename,'w') as balance:
        balance.write('{:.2f\n}'.format(new_balance))
    with open('log.txt','a') as log:
        timestamp = datetime.now().strftime("%d.%b %Y %H:%M:%S")
        log.write('{},customer:{}\n'.format(timestamp,name))
        log.write('transactiontype:deposit:amt:{.2f\n}'.format(deposit))

def withdraw():
    name = input("Enter customer name: ")
    filename = '{}.txt'.format(name)
    withdraw = float(input("Enter the withdrawal amount: "))
    with open(filename,'r') as balance:
        current_balance = balance.readline()
        number = current_balance.rstrip()
        new_balance = float(number) - withdraw
        print('Your new balance is ${:.2f}'.format(new_balance))
    with open(filename,'w') as balance:
        balance.write('{:.2f\n}'.format(new_balance))
    with open('log.txt','a') as log:
        timestamp = datetime.now().strftime("%d.%b %Y %H:%M:%S")
        log.write('{},customer:{}\n'.format(timestamp,name))
        log.write('transactiontype:withdraw:amt:{.2f\n)'.format(withdraw))

selection = None
while selection != 4:
    selection = menu()
    if selection == 1:
        createAccount()
    elif selection == 2:
        deposit()
    elif selection == 3:
        withdraw()
    print()

When running the code: Press 1 to create account Enter the username Enter initial deposit Then the error kicks in.

3
  • 1
    You seem to have \n inside of format specifiers Commented Feb 4, 2020 at 22:59
  • You can use multiple open() on the same line, btw. open('1') as f, open('2') as f2 Commented Feb 4, 2020 at 23:01
  • @AlexSkalozub That was the main issue for formatting not working. Thanks. Commented Feb 4, 2020 at 23:31

1 Answer 1

3

It is because in the with statement there

with open(filename,'w') as balance:
    print('The balance is ${:.2f}'.format(balance))
    balance.write('{:.2f}\n'.format(balance))

balance has become a file handle. The previous user input has been overwritten.

Of course the format cannot turn it into a .2f string.

Change the line to something like

  with open(filename,'w') as balance_file:

should fix it.

Sign up to request clarification or add additional context in comments.

2 Comments

oh I see there. I have the file object as balance and the previous balance input variable as same.
That's the root cause. Please don't forget to accept the answer. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.