您好新编码并且遇到python的一些问题我得到此错误
C:\Users\\Desktop\exam review\review 1.py", line 89, in main
endBalance = begBalance - totalWithdrawals + totalDeposits
TypeError: unsupported operand type(s) for +: 'float' and 'str'
以下是代码:
def main():
#Declare and initialize variables
#float = 0.0, with1 =0.0, with2 = 0.0, with3 = 0.0
with1 = with2 = with3 = 0.0
#float deposit1 = 0.0, deposit2 = 0.0, deposit3 = 0.0
deposit1 = deposit2 = deposit3 = 0.0
#float totalWithdrawals = 0.0, totalDeposits = 0.0, endBalance = 0.0, begBalance = 0.0
totalWithdrawals = totalDeposits = endBalance = begBalance = 0.0
#string acctNumber = “ “
acctNumber = ""
#string firstName = “ ”, lastName = “ ”
firstName = lastName = ""
#int age = 0
age = 0
#string userName =” “, convertedAcctNumber = “ “
userName = convertedAcctNumber = ""
#Intro
print("This program will show your bank statement\n")
#Open file “January.txt” for input
infile = open("January.txt", "r")
#Read in line1 and store in acctNumber
line1 = infile.readline()
acctNumber = line1
#Read in line2 and store in begBalance
line2 = infile.readline()
begBalance = line2
#Convert begBalance to float
begBalance = float(begBalance)
#Read in line3 and split into with1, with2, with3
line3 = infile.readline()
with1, with2, with3 = line3.split(",")
#Convert with1, with2, and with3 to float
with1 = float(with1)
with2 = float(with2)
with3 = float(with3)
#Read in line4 and split into deposit1, deposit2, deposit3
line4 = infile.readline()
deposit1, deposit2, deposit3 = line4.split(",")
#Convert deposit1, deposit2 and deposit3 into float
depoit1 = float(deposit1)
depoit2 = float(deposit2)
depoit3 = float(deposit3)
#Close file
infile.close()
#Prompt for firstName
firstName = input("What is your first name: ")
#Prompt for lastName
lastName = input("What is your last name: ")
#Prompt for age
age = eval(input("What is your age: "))
#Use string manipulation to create username
userName = (firstName[0] + lastName[:5] + str(age)).lower()
#Use string manipulation to create convertedAcctNumber
convertedAcctNumber = acctNumber[0:3] + "-" + acctNumber[3:5] + "-" + acctNumber[5:]
#Calculate totalDeposits
totalDeposits = deposit1 + deposit2 + deposit3
#Calculate totalWithdrawals
totalWithdrawals = with1 + with2 + with3
#Calculate endBalance
endBalance = begBalance - totalWithdrawals + totalDeposits
#Display report on screen
print("Broward College National Bank".center())
print("Name \t\t\t UserName\t\t\t Account Number")
print("-----------------------------")
#Open “January statement.txt” for output
#Write the report to the file
#Close file `
因此,由于我拥有非常有限的编程知识,我尝试了以下方法将所有内容都设置为浮点数(我已经将deposit1 deposit2 deposit3和with1 with2 with3放入浮点数,所以当它们被添加时,totalDeposits和totalWithdrawls会变成浮点数自己,使以下多余?)
'#Calculate totalDeposits
totalDeposits = deposit1 + deposit2 + deposit3
totalDeposits = float(totalDeposits)
#Calculate totalWithdrawals
totalWithdrawals = with1 + with2 + with3
totalWithdrawals = float(totalWithdrawals)
#Calculate endBalance
endBalance = begBalance - totalWithdrawals + totalDeposits
endBalance = float(endBalance) '
然后我收到此错误
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
main()
File "C:\Users\\Desktop\exam review\review 1.py", line 82, in main
totalDeposits = float(totalDeposits)
ValueError: could not convert string to float: '200.56234.56352.35\n'
这些是readin line4的价值.. sooo我几乎卡住了,不知道还能做什么,谢谢大家的帮助。
答案 0 :(得分:2)
你做;
depoit1 = float(deposit1)
然后使用deposit1
,就像它是一个浮点数一样。请注意拼写错误depoit1
&lt;&gt; deposit1
答案 1 :(得分:0)
错误非常清楚,您正在尝试向浮点数添加字符串: '200.56234.56352.35 \ N'