2014-11-07 53 views
0

下面的代碼雖然有些雜亂,但要求用戶輸入:退休前的年數,利率,初始金額和每年添加的金額。這部分工作正常。我現在要做的是根據用戶的輸入每五年計算一次價值。基本上我希望它看起來是這樣的:從用戶輸入中用Python計算IRA /退休

The value of your account after 5 years will be $... 
The value of your account after 10 years will be $... 
The value of your account after 15 years will be $... 
etc... all the way up until the number of years_left is met 

的代碼我現在主要進行的是除了我想我要麼有一個數學錯誤或編碼錯誤(或兩者),我似乎無法人物出。任何幫助將不勝感激。我的代碼如下:

def main(): 

    # Number of years left until retirement 
    while True: 
     try: 
      years_left = int(input("Please enter the number of years left until retirement (1-70): ")) 
     except ValueError: 
      print ("Entered value is not a number! Please enter a number 1-70.") 
     except KeyboardInterrupt: 
      print("Command Error! Please enter a number 1-70.") 
     else: 
      if 1 <= years_left < 70: 
       break 
      else: 
       print("Entered value is not in the range 1-70.") 

    # Interest rate (confirmation needed if greater than 10) 
    while True: 
     try: 
      interest_rate = int(input("Please enter an interest rate: ")) 
      if interest_rate > 10: 
       confirm = input("Entered interest rate is greater than 10%. Are you sure? (y/n): ") 
       if confirm =="y": 
        break 
      elif 0 <= interest_rate < 10: 
       break 
     except ValueError: 
      print("Entered value is not a number! ") 

    # Initial amount to the IRA 
    while True: 
     try: 
      initial_amount = int(input("Please enter the initial amount to the IRA: ")) 
     except ValueError: 
      print ("Entered value is not a number! Please enter a number.") 
     except KeyboardInterrupt: 
      print("Command Error! Please enter a number.") 
     else: 
      if 0 < initial_amount: 
       break 
      else: 
       print("Entered value is a negative number. Please enter a positive number.") 

    # Amount added to the IRA each year 
    while True: 
     try: 
      amount_added = int(input("Please enter the amount added to the IRA each year: ")) 
     except ValueError: 
      print ("Entered value is not a number! Please enter a number.") 
     except KeyboardInterrupt: 
      print("Command Error! Please enter a number.") 
     else: 
      if 0 <= amount_added <= 2500: 
       break 
      else: 
       print("Entered amount is not in the range $0 - $2,500.") 

    #value = initial_amount + (initial_amount)*interest_rate + amount_added 

    value = 0 
    for x in range(5, years_left + 5): 
     value = initial_amount + (initial_amount) * interest_rate + amount_added 
     print("The value of your account after " + str(x) + " years will be $" + str(value)) 

main() 

我現在的程序輸出是這樣的:

Please enter the number of years left until retirement (1-70): 10 
Please enter an interest rate: 7 
Please enter the initial amount to the IRA: 1 
Please enter the amount added to the IRA each year: 2000 
The value of your account after 5 years will be $2008 
The value of your account after 6 years will be $2008 
The value of your account after 7 years will be $2008 
The value of your account after 8 years will be $2008 
The value of your account after 9 years will be $2008 
The value of your account after 10 years will be $2008 
The value of your account after 11 years will be $2008 
The value of your account after 12 years will be $2008 
The value of your account after 13 years will be $2008 
The value of your account after 14 years will be $2008 

回答

1

更改爲:

value = 0 
for x in range(5, years_left+5): 
    value = initial_amount + (initial_amount) * interest_rate + amount_added 
    print("The value of your account after " + str(x) + " years will be $" + str(value)) 

這樣的:

count=5 
value = 0 
for x in range(5, years_left): 
    value = value + initial_amount + (initial_amount) * interest_rate + amount_added 
    print("The value of your account after " + str(count) + " years will be $" + str(value)) 
    count += 5 
    if count > year_left+5: 
     break 

你的情況你採取了範圍(5,year_left + 5),它會ge nerarate一個5至year_left + 5點亮數的,所以它會像[5,6,7,8,9.....year_left+5],它不會通過5

更Python遞增將是:

for x in range(5, years_left + 5,5): 
    value = value + initial_amount + (initial_amount) * interest_rate + amount_added 
    print("The value of your account after " + str(x) + " years will be $" + str(value)) 

將通過5增量走

+0

這將解決我的計數問題,但它仍然每次都給出不正確的值。 – Ben 2014-11-07 01:52:32

+0

現在檢查你需要做的價值太'value = value + your_stufff' – Hackaholic 2014-11-07 01:53:22

+0

@Ben希望這解決了你的問題,你得到的概念 – Hackaholic 2014-11-07 01:57:56