05.PB Python While Loop Exercise
05.PB Python While Loop Exercise
Problems for exercise and homework for the course "Programming Basics" course @ SoftUni Global
Submit your solutions to the Softuni Judge system: https://judge.softuni.org/Contests/4586
1.Old Books
Annie goes to her hometown after a very long period out of the country. After coming home, she sees her
grandmother's old library and remembers her favorite book. Help Annie by writing a program in which she enters
the book (string) she is looking for. Until Annie finds her favorite book or checks out all the books in the library,
the program must read the name of each following book (string). The books in the library are finished once you
get the text "No More Books".
If she does not find the book, print in two lines:
o "The book you search is not here!"
o "You checked {count} books."
If she finds the book, print on a single line:
o "You checked {count} books and found it."
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
3. Make a while loop where you read a new book from the console each time until the books in the library run
out and read the text "No More Books".
4. Make a check - if the book you are reading from the console matches Annie's favorite book, change the
value of the boolean variable to True, print and exit the loop. Otherwise, increment the counter by one:
5. According to whether the book is not found, print the necessary messages:
2. Exam Preparation
Write a program with which Michael solves problems from his exams until he receives the "Enough" command from
his lecturer. For each problem he solves, he receives a grade. The program should finish when receiving the
"Enough" command or if Michael receives the specified number of poor grades.
A poor grade is any grade that is less than or equal to 4.
Input Data
On the first line - number of poor grades – an integer;
Then multiple readings of two lines:
o Problem name – string
o Grade – an integer in the range [2…6]
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
3. Create a while loop that continues until the number of poor grades is less than the number you read from
the console. On each iteration of the loop, read the name of the problem and its grade.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
6. After the loop, if the number of poor grades has reached the maximum poor grades, print the appropriate
message:
3. Vacation
Jessie has decided to raise money for a vacation and wants you to help her find out if she will be able to collect the
required amount. She saves or spends some of her money every day. If she wants to spend more than her
available money, she will spend everything she has and will be left with 0 USD.
Input Data
From the console read:
Money needed for the vacation – a floating-point number.
Available money – a floating-point number.
Then 2 lines are read multiple times:
Action type – text with "spend" and "save" options.
Amount to save/spend – a floating-point number.
Output Data
The program must end in the following cases:
If in 5 consecutive days Jessie only spends, the console displays:
o "You can't save the money."
o "{Total days passed}"
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
2. Make two helper variables at the beginning to keep track of the number of days passed and the number of
consecutive days Jesse spends money. Let both variables have an initial value of zero:
3. Create a while loop that continues until Jessie's money is less than the money she needs for the trip and the
counter for consecutive days is less than 5. At each iteration of the loop, read two lines from the console - the
first line is text - spend or save, and the second is the money Jessie has saved or spent. Also, increase the days
counter by 1:
5. After the loop, check whether Jessie has spent money on five consecutive days and print the message. Also
check whether Jessie has collected the money and, if she has, print the appropriate message
4. Walking
Emma wants to start a healthy lifestyle and has set a goal to walk 10,000 steps every day. However, some days she
is very tired from work and will want to get home before she reaches her goal. Write a program that reads from the
console how many steps she walks each time she goes out during the day, and when she reaches her goal, print
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
1000 Goal reached! Good job! 1500 2500 more steps to reach goal.
1500 1000 steps over the goal! 300
2000 2500
6500 3000
Going home
200
1500 Goal reached! Good job! 125 Goal reached! Good job!
3000 298 steps over the goal! 250 1765 steps over the goal!
250 4000
1548 30
2000 2678
Going 4682
home
2000
5. Coins
Vending machine manufacturers wanted to make their machines return as few change coins as possible. Write a
program that receives an amount - the change that needs to be returned and calculates with how few coins this
can be done. The coins can be 2.00 EUR, 1.00 EUR, 0.50 EUR, 0.20 EUR, 0.10 EUR, 0.05 EUR, 0.02 EUR, 0.01 EUR.
1.23 4 Our change is 1,23 EUR. The machine returns it to us with 4 coins: a coin of 1 EUR, a coin of
0.20 EUR, a coin of 0.02 EUR, and a coin of 0.01 EUR.
2 1 Our change is 2.00 EUR. The machine returns it to us with 1 coin of 2 EUR.
0.56 3 Our change is 0.56 EUR. The machine returns it to us with 3 coins: a coin of 0.50 EUR, a coin
of 0.05 EUR, and a coin of 0.01 EUR.
2.73 5 Our change is 2.73 EUR. The machine returns it to us with 5 coins: a coin of 2 EUR, a coin of
0.50 EUR, a coin of 0.20 EUR, a coin of 0.02 EUR, and a coin of 0.01 EUR.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
6. Cake
You're invited to a 30th birthday party, where the birthday boy serves a huge cake. However, he doesn't know how
many pieces the guests can take from it. Your task is to write a program that calculates the number of pieces the
guests have taken before it is eaten. You will receive the dimensions of the cake (width and length – integers) and
then on each line until you receive the "STOP" command or until the cake is finished, the number of pieces the
guests take from it. Each piece of cake is 1x1 cm.
"No more cake left! You need {number of not enough pieces} pieces more."
7. Moving
On Jose's eighteenth birthday, he decided that he was going to move out to live in an apartment. He packed his stuff
in boxes and found an ad for a suitable apartment to rent. He began moving his stuff in pieces because he couldn't
carry it all at once. He has limited free space in his new apartment where he can place his stuff so that the place is
suitable for living.
Write a program that calculates the free volume of Jose's apartment that remains after he moves his stuff.
Each box is of exact size: 1m. x 1m. x 1m.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Output Data
Print one of the following lines on the console:
- If you receive the "Done" command and there is still space available:
"{number of free cubic meters} Cubic meters left."
- If the free space runs out before the "Done" command is received:
"No more free space! You need {number of not enough cubic meters} Cubic meters more."
10 No more free space! You need 2 Cubic 10 * 10 * 2 = 200 cubic meters available
10 meters more. 20 + 20 + 20 + 20 + 122 = 202 cubic meters
2 200 - 202 = 2 needed cubic meters
20
20
20
20
122
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.