94. Python Program to Count the Weeks to Reach a Savings Goal
MediumEndless loops
Overview
A loop until a goal is reached, with a check that stops an infinite loop before it starts.
Problem Statement
Sunny saves the same amount every week. Read the goal, then the weekly saving (both whole numbers). Each week, add the saving to the total and print the week and the total. When the total reaches the goal or more, print how many weeks it took. For example, for a goal of 1000 and 300 a week:
Week 1: 300
Week 2: 600
Week 3: 900
Week 4: 1200
Weeks needed: 4
If the weekly saving is 0 or less, the goal is never reached, so the loop would run forever. In that case print only:
Saving must be more than 0
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
One value per line, in this order: the goal, then the weekly saving. Example:
1000
300
Output
The output for that example:
Week 1: 300
Week 2: 600
Week 3: 900
Week 4: 1200
Weeks needed: 4
The tests try 4 different inputs.