0

I tried reading and saving a file's contents as a variable, tried converting it into a float and-

Traceback (most recent call last):
  File "/Users/username/wageCalculator.py", line 26, in <module>
    writeTotal.write(str(float(total)+float(getBal)))
TypeError: float() argument must be a string or a number, not '_io.TextIOWrapper

Heres the edited code:(changed directory names) age

import time
while True:
    wage = 5
    moneyPerSecond=(int(wage)/3600)
    moneyPerMinute=(int(wage)/60)
    print('Your wage per second is: $'+str(moneyPerSecond))
    print('Please input the amount of minutes you worked. It must be a whole number.')
    minutesWorked = input()
    minuteWage = int(minutesWorked)*float(moneyPerMinute)
    print('Please input the amount of seconds you worked. It can be a decimal.')
    secondsWorked = input()
    secondWage = float(secondsWorked)*float(moneyPerSecond)
    print('Your total is: $' + str(float(minuteWage)+float(secondWage)))
    total = float(minuteWage)+float(secondWage)
    getBal = open('/Users/username/balance.txt','r+')
    readBal = getBal.read()
    getBal.close()
    print('Current balance: $' + str(readBal))
    print('Do you want to add this to your balance?')
    print('Key in "y" for yes or "n" for no and press enter:')
    yesOrNo = input()
    if yesOrNo=='y':
        open('/Users/username/balance.txt','w').close
        writeTotal = open('/Users/username/balance.txt' ,'a')
        writeToLog = open('/Users/username/balanceLog.txt','a')
        writeToLog.write(str(total))
        writeTotal.write(str(float(total)+float(readBal)))
        writeTotal.close()
        writeToLog.close()
    elif yesOrNo=='n':
        print('Reseting...')
        time.sleep(1)
    else:
        print('Please try again.')

I have tried converting it into a string but it gives me the same error or so.

It would be great anyone could find out an answer. :p

Edit: I have tried getBal and readBal, and readBal gives me this error:

Traceback (most recent call last):
  File "/Users/Jonathan_Xu/wageCalculator.py", line 27, in <module>
    writeTotal.write(str(float(total)+float(readBal)))
ValueError: could not convert string to float:
  • `getBal` is a file handler, not a string. Do you mean `readBal`? – Loocid Nov 26 '18 at 03:00
  • possibly dupes https://stackoverflow.com/questions/43580868/ (check John's answer for clarification). also i *highly* suggest using `with` to open files: https://stackoverflow.com/questions/3012488/. cheers :) – MCO Nov 26 '18 at 03:01

1 Answers1

1

The line of code in the error does not match the posted code:

writeTotal.write(str(float(total)+float(getBal)))

vs.

writeTotal.write(str(float(total)+float(readBal)))

Presumably you meant to use float(readBal).

John Gordon
  • 29,573
  • 7
  • 33
  • 58