Python Code that predicts the amount of money you will have in your IRA when you
ID: 3765848 • Letter: P
Question
Python Code that predicts the amount of money you will have in your IRA when you retire. Inputs are: 1. Number of years until you retire, it must be a whole number (integer) in the range 1 to 70. Any number outside of this range is to be rejected and the user will be required to enter the value again, this repeat must be done until a valid number is input. 2. Expected interest rate as a percent (this the average expected earnings per year until retirement is reached). The number must be 0 or greater, any other value will be rejected and as above, the user must be polled until a valid number is input. If the number is greater than 10%, the user must be asked if he/she really expects to earn this much, if the user replies in the affirmative, the number is to be used, otherwise the user will be asked to input the value again and the above checks will be made. 3. The initial amount in the IRA. This is a decimal number and may be any non-negative number. If a negative number is entered, the value is to be rejected and the user will be required to enter an acceptable value. Again, loop until a valid value is input. 4. The amount expected to be added to the IRA each year. This is a decimal number in the range of 0 to $2,500. The program must loop asking for this value until a valid value is entered. After the data is input and validated, the program is to loop printing out the value of the IRA every 5 years and the value at the end of the time entered as input #1. The value is to be computed in a loop where the value each year is determined by: value = value + (value)*rate + yearlyInput
Explanation / Answer
Here is the code for you. If you have any further queries, just get back to me.
#! /usr/bin/python
numOfYears = input("Number of years until you retire: ");
print numOfYears;
while(numOfYears < 1 or numOfYears > 70):
print "Invalid number. Should be within the range 1 - 70.";
numOfYears = input("Number of years until you retire: ");
affirm = 0;
while(not affirm):
percent = input("Expected interest rate as a percent: ");
while(percent < 0):
print "Interest rate cannot be negative.";
percent = input("Expected interest rate as a percent: ");
if(percent > 10):
affirm = input("Do you really expects to earn this much (1 / 0): ");
else:
affirm = 1;
rate = percent / 100.0;
initialAmount = input("Enter the initial amount in the IRA: ");
while(initialAmount < 0):
print "Initial amount in IRA cannot be negative.";
initialAmount = input("Enter the initial amount in the IRA: ");
yearlyAmount = input("Enter the amount expected to be added to the IRA each year: ");
while(yearlyAmount < 0 or yearlyAmount > 2500):
print "You can add amount within the range 0 - $2500.";
yearlyAmount = input("Enter the amount expected to be added to the IRA each year: ");
i = 1;
value = initialAmount;
while(i <= numOfYears):
value = value + (value)*rate + yearlyAmount;
if(i % 5 is 0):
print "Value of IRA after ",i," years is: ",value;
i = i + 1;
print "Value of IRA after ",i," years is: ",numOfYears,value;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.