0% found this document useful (0 votes)
66 views

05.PB Python While Loop Exercise

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

05.PB Python While Loop Exercise

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Exercise: While Loop

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."

Sample Input and Output


Input Output Comments
Troy You checked 2 books and found it. The book Annie is looking for in this case
Stronger is Troy, and the library contains 3 books.
Life Style The first one is Stronger, the second one
Troy is Life Style, the third book is the
searched one - Troy and the program
ends.
The Spot The book you search for is not The book Annie is looking for is The
Hunger Games here! Spot.
Harry Potter You checked 4 books.
Torronto The library contains 4 books.
Spotify The first one is Hunger Games, the
second one is Harry Potter, the third
No More Books
one is Torronto, and the fourth one is
Spotify. Since there are no more books
in the library, the name reading
finished. Annie didn't find the book she
was looking for.
Bourne You checked 10 books and found it.
True Story
Forever
More Space
The Girl
Spaceship
Strongest
Profit
Tripple
Stella
The Matrix
Bourne

© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 1 of 9


Hints and Guidelines
1. Read the input data from the console:

2. Make two more helper variables at the beginning:


 one will be a counter (of integer type and with an initial value of zero), which will keep track of how
many books have been checked;
 the other variable must be of boolean type and have an initial value False.

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:

Testing in the Judge System


Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4586#0

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.

Follow us: Page 2 of 9


Output Data
 If Michael receives the "Enough" command, print on 3 lines:
o "Average score: {average score}"
o "Number of problems: {number of all problems}"
o "Last problem: {last problem name}"
 If he receives the specified number of poor grades:
o "You need a break, {number of poor grades} poor grades."
The average score must be formatted to two digits after the decimal point.

Sample Input and Output


Input Output Comments
3 Average score: 5.25 The number of poor grades allowed is 3.
Money Number of problems: 4 The first problem is called Money, Michael's grade is 6.
6
Last problem: Bus The second problem is Story, Michael's grade is 4.
Story
4 The third problem is Spring Time, Michael's grade is 5.
Spring Time The fourth problem is Bus, Michael's grade is 6.
5 The next command is Enough, and the program ends.
Bus Average score: 21 / 4 = 5.25
6
Enough Number of solved problems: 4
Last problem: Bus
Input Output Comments
2 You need a break, 2 poor The number of poor grades allowed is 2.
Income grades. The first problem is called Income, Michael's grade is 3.
3
Game Info The second problem is Game Info, Michael's grade is 6.
6 The third problem is Best Player, Michael's grade is 4.
Best Player Michael has reached the allowed number of poor
4 grades, time for a break.

Hints and Guidelines


1. Read the input data from the console:

2. Make five helper variables at the beginning:


 counter for solved problems - with an initial value of 0;
 counter for poor grades – with an initial value of 0;
 sum of all grades - with initial value 0;
 which is the last problem - with initial value blank text;
 whether or not he failed;

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.

Follow us: Page 3 of 9


4. In case you receive an Enough command change the value of has_failed to False and break the loop;
5. At each loop iteration, add Marin's grade to the sum of all his grades and increase the problems counter. If
the grade is less than or equal to 4 increase the counter for poor grades. Rewrite the name of the last
problem;

6. After the loop, if the number of poor grades has reached the maximum poor grades, print the appropriate
message:

Testing in the Judge System


Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4586#1

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.

Follow us: Page 4 of 9


 If Jessie collects the money for the vacation, the console displays:
o "You saved the money for {Total days passed} days."

Sample Input and Output


Input Output Comments
2000 You saved the Money needed for the vacation: 2000
1000 money for 2 days. Available money: 1000
spend
spend - subtract the next number from the money
1200
save (1000 - 1200 = -200, which is less than 0
2000 => available money = 0)
~ consecutive days spend = 1
- total days: 1
save - add the next number to the money
(0 + 2000 = 2000)
~ consecutive days spend = 0
- total days: 2
Available money (2000) >= Money needed for the vacation (2000)
110 You can't save Money needed for the vacation: 110
60 the money. Available money: 60
spend 5 spend – subtract the next number from the money (60 - 10 = 50)
10 ~ consecutive days spend = 1
spend - total days: 1
10 spend – subtract the next number from the money (50 - 10 = 40)
spend ~ consecutive days spend = 2
10 - total days: 2
spend spend – subtract the next number from the money (40 - 10 = 30)
10
~ consecutive days spend = 3
spend
- total days: 3
10
spend – subtract the next number from the money (30 - 10 = 20)
~ consecutive days spend = 4
- total days: 4
spend – subtract the next number from the money (20 - 10 = 10)
~ consecutive days spend = 5
- total days: 5
5 consecutive days spend => available money: 10
Available money (10) < Money needed for the vacation (110)
250 You saved the Money needed for the vacation: 250
150 money for 4 days. Available money: 150
spend spend - subtract the next number from the money (150 - 50 = 100)
50 ~ consecutive days spend = 1
spend - total days: 1
50 spend - subtract the next number from the money (100 - 50 = 50)
save ~ consecutive days spend = 2
100 - total days: 2
save save - add the next number to the money (50 + 100 = 150)
100
~ consecutive days spend = 0
- total days: 3
save - add the next number to the money (150 + 100 = 250)
~ consecutive days spend = 0
- total days: 4
Available money (250) >= Money needed for the vacation (250)

© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 5 of 9


Hints and Guidelines
1. Read the input data from the console:

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:

4. Check whether Jessie is spending or saving for the day:


a. If she saves, add the money saved to hers and reset the counter for consecutive days;
b. If she spends, subtract from her money the amount she spent and increase the counter for the
consecutive days she spends. Check if Jessie's money has become less than zero and if so, she is out of
money and has zero leva;

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

Testing in the Judge System


Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4586#2

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.

Follow us: Page 6 of 9


"Goal reached! Good job!" and how many more steps she has walked "{difference between steps} steps
over the goal!"
If she wants to go home before then, she will enter the command "Going home" and enter the steps she has
walked while going home. Then, if she has failed to reach her goal, the console should print: "{difference between
steps} more steps to reach the goal."

Sample Input and Output


Input Output Input Output

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

Input Output Input Output

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

Testing in the Judge System


Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4586#3

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.

Sample Input and Output


Input Output Comments

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.

Follow us: Page 7 of 9


Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4586#4

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.

Print one of the following lines on the console:


 "{number of pieces} pieces are left." - if you receive STOP and have not finished the cake pieces

 "No more cake left! You need {number of not enough pieces} pieces more."

Sample Input and Output


Input Output Comments
10 No more cake left! You need 1 pieces more. The cake is 10 cm long and 10 cm wide
10 => number of pieces = 10 * 10 = 100
20 1st take -> 100 - 20 = 80
20 2nd take -> 80 - 20 = 60
20 3rd take -> 60 - 20 = 40
20 4th take -> 40 - 20 = 20
21 5th take -> 20 - 21 = -1 < 0
=> no more cake left, 1 piece is not enough
10 8 pieces are left. The cake is 10 cm long and 2 cm wide
2 => number of pieces = 10 * 2 = 20
2 1st take -> 20 - 2 = 18
4 2nd take -> 18 - 4 = 14
6 3rd take -> 14 - 6 = 8
STO 4th take -> command STOP
P => pieces left: 8

Testing in the Judge System


Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4586#5

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.

Follow us: Page 8 of 9


Input Data
The user enters the following data on separate lines:
1. Free space width - an integer
2. Free space length - an integer
3. Free space height - an integer
4. On the next lines (until the "Done" command is received) – the number of boxes to be moved to the
apartment - integers
The program should finish by receiving the "Done" command or if the free space runs out.

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."

Sample Input and Output


Input Output Comments

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

10 10 Cubic meters left. 10 * 1 * 2 = 20 cubic meters available


1 4 + 6 = 10 cubic meters
2 20 - 10 = 10 cubic meters
4
6
Done

Testing in the Judge System


Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4586#6

© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.

Follow us: Page 9 of 9

You might also like