2014-02-17 48 views
-1

你好新的編碼,並具有與蟒蛇IM一些問題得到這個錯誤爲+不支持的操作類型:「浮動」和「STR」

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' 

這些都是閱讀下載4號線的值.. SOOO即時通訊幾乎被卡住,不知道自己能做什麼,謝謝大家的幫助。

回答

2

你這樣做;

depoit1 = float(deposit1) 

然後使用deposit1就好像它是一個浮動。注意錯字,depoit1 <>deposit1

+1

與同爲'depoit2'和'depoit3 '當然。 – abarnert

+0

哈哈得倍加關注,謝謝你的幫忙! – user3320792

0

的錯誤是很清楚的,你想字符串添加到一個float: '200.56234.56352.35 \ n'

相關問題