我尝试读取文件的内容并将其保存为变量,并尝试将其转换为float和-
-3
在此编辑代码:(更改的目录名称) 年龄
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
我尝试将其转换为字符串,但它给了我同样的错误。
任何人都能找到答案,那就太好了。 :p
编辑:我已经尝试过getBal和readBal,而readBal给了我这个错误:
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.')
答案 0 :(得分:1)
错误中的代码行与发布的代码不匹配:
writeTotal.write(str(float(total)+float(getBal)))
vs。
writeTotal.write(str(float(total)+float(readBal)))
大概您打算使用float(readBal)
。