Question

In: Computer Science

1. Write Python program that use a while loop to determine how long it takes for...

1. Write Python program that use a while loop to determine how long it takes for an investment to double at a given interest rate.

The input will be an annualized interest rate, and the output is the number of years it takes an investment to double.

Note: The amount of initial investment can be any positive value, you can use $1.

2. Write a function def DoubleInvest(initialInvest) to perform the same functionalities as question1. Make sure to include you main function for testing.

Solutions

Expert Solution

Given below is the code for question. Please indent code as shown in image.


Answer 1)
=====
initialAmt = 1
rate = float(input('Enter interest rate: '))
finalAmount = initialAmt
years = 0

while finalAmount < 2 * initialAmt:
   years = years + 1
   interest = finalAmount * rate / 100.0
   finalAmount = finalAmount + interest
  

print('At an interest rate of {:.2f}, your initial investment of ${:.2f} will be ${:.2f} in {} years'.format(rate, initialAmt, finalAmount, years))

Answer 2)
=====
def DoubleInvest(initialInvest):

   rate = float(input('Enter interest rate: '))
   finalAmount = initialInvest
   years = 0

   while finalAmount < 2 * initialInvest:
       years = years + 1
       interest = finalAmount * rate / 100.0
       finalAmount = finalAmount + interest
      

   print('At an interest rate of {:.2f}, your initial investment of ${:.2f} will be ${:.2f} in {} years'.format(rate, initialInvest, finalAmount, years))

def main():
   initialInvest = 1
   DoubleInvest(initialInvest)

if __name__ == "__main__":
   main()

output
------
Enter interest rate: 5
At an interest rate of 5.00, your initial investment of $1.00 will be $2.08 in 15 years


Related Solutions

Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Python Program 1: Write a program that takes user input in the form of a string...
Python Program 1: Write a program that takes user input in the form of a string Break up the string into a list of characters Store the characters in a dictionary The key of the dictionary is the character The value of the dictionary is the count of times the letter appears Hint: remember to initialize each key before using Output of program is the count of each character/letter Sort the output by key (which is the character) Python Program...
Design a Python program with a While loop that lets the user enter a number. The...
Design a Python program with a While loop that lets the user enter a number. The number should be multiplied by 10 and the result stored in a variable named "product". The loop should repeat as long as the product variable < 10. Anything not completely obvious to a newbie should be commented on as in line comments. Should be modulated and repeatable as well as there should be an invocation of the main routine at the end of the...
1 for each of the problems listed below write the c++ program while using WHILE loop...
1 for each of the problems listed below write the c++ program while using WHILE loop structures A program will display each term in the following sequence 1 -10 100 -1000 10000 -100000 1000000 A program will calculate and display the corresponding celsius temperatures for the given Farenheit ones from 0f to 212 f (hint c=(f-32/1.8)
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT