livepython.net

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.

Learn this firstChapter 11 · Infinite Loops in Python and How to Avoid Them
solution.py
# write your code here

EXPECTED OUTPUT

1Week 1: 300
2Week 2: 600
3Week 3: 900
4Week 4: 1200
5Weeks needed: 4

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.