2017-04-07 149 views
0
def main(): 
    account_value = input("Enter your account value: ") 
    AV = account_value 
    years = input("Enter how many years you want to save: ") 
    IR = input("enter the interest rate per year: ") 
    for i in range(int(years)): 
     aftervalue = int(AV) + (float(IR)*int(AV)) 
    print(aftervalue) 

爲什麼我必須將它們轉換爲循環中的表達式?爲什麼我必須轉換變量?

爲什麼他們已經被視爲整數和浮點數?

+2

在Python 3中,'input()'返回一個字符串,它不解析輸入。 – Barmar

+0

爲什麼在一個循環中爲同一個變量賦值相同的變量?每次都是一樣的。 – Barmar

+0

不要將它們轉換成循環,在分配變量時將它們轉換一次,例如'年= int(輸入(「輸入年份:」))' – Barmar

回答

0

在Python 3中,input()返回一個字符串。如果你想在算術運算中使用它,你必須首先將它轉換成intfloat

你不需要在循環中完成它,最好在分配變量時只做一次。

如果您在Python 2中運行腳本,則不需要執行轉換。它的input()函數將輸入評估爲Python表達式,您必須使用raw_input()將原始輸入作爲字符串獲取。

+0

謝謝 '高清的main():' \t'account_value =輸入(「請輸入您的賬戶價值:」)' \t (輸入(「輸入你想要保存多少年:」))'\t'IR = float(input(「輸入每年的利率: 「))' \t'我在範圍內(年):' \t \t'aftervalue = AV + (IR * AV)' \t'打印(aftervalue)' \t \t' 現在看起來這和它的作品依然。它也更有意義。主要的是我不知道輸入處理所有的字符串(我也不知道如何在註釋中正確地格式化代碼):( –

+0

不要在註釋中張貼代碼,沒有格式。 – Barmar

相關問題