automated terminal push

This commit is contained in:
2025-06-05 07:33:41 -04:00
parent 19decb39cc
commit 1cb989b04d
619 changed files with 10585 additions and 0 deletions

View File

@@ -0,0 +1,273 @@
# Chapter 7.2. More Complex Loops Exam Problems
We **already learned** how to execute **a block of commands more than once** using a **`for`** loop. In the previous chapter, **we reviewed some loop structures** that would help us solve more complex problems like:
- **loops with a step**
- **nested** loops
- **`while`** loops
- **`do-while`** loops
- **infinite** loops and breaking out of the loop (**`break`** operator)
- **`try-except`** construction
## Exam Problems
Let's start work on solving the following practical exam problems.
## Problem: Stupid Password Generator
Write a program that enters two integers **n** and **l** and generates in alphabetical order all possible **"dumb” passwords"**, that consist of the following **5 characters**:
- Character 1: digit from **1** to **n**.
- Character 2: digit from **1** to **n**.
- Character 3: small letter among the first **l** letters of the Latin alphabet.
- Character 4: small letter among the first **l** letters of the Latin alphabet.
- Character 5: digit from **1** to **n, bigger than first 2 digits**.
### Input Data
The input is read from the console and consists of **two integers: n** and **l** within the range [**1 … 9**], one per line.
### Output Data
Print on the console **all "dumb" passwords in alphabetical order**, separated by **space**.
### Sample Input and Output
|Input|Output|Input|Output|
|---|---|---|---|
|2<br>4|11aa2 11ab2 11ac2 11ad2 11ba2 11bb2 11bc2 11bd2 11ca2 11cb2 11cc2 11cd2 11da2 11db2 11dc2 11dd2|3<br>1|11aa2 11aa3 12aa3 21aa3 22aa3|
|Input|Output|Input|Output|
|---|---|---|---|
|3<br>2|11aa2 11aa3 11ab2 11ab3 11ba2 11ba3 11bb2 11bb3 12aa3 12ab3 12ba3 12bb3 21aa3 21ab3 21ba3 21bb3 22aa3 22ab3 22ba3 22bb3|4<br>2|11aa2 11aa3 11aa4 11ab2 11ab3 11ab4 11ba2 11ba3 11ba4 11bb2 11bb3 11bb4 12aa3 12aa4 12ab3 12ab4 12ba3 12ba4 12bb3 12bb4 13aa4 13ab4 13ba4 13bb4 21aa3 21aa4 21ab3 21ab4 21ba3 21ba4 21bb3 21bb4 22aa3 22aa4 22ab3 22ab4 22ba3 22ba4 22bb3 22bb4 23aa4 23ab4 23ba4 23bb4 31aa4 31ab4 31ba4 31bb4 32aa4 32ab4 32ba4 32bb4 33aa4 33ab4 33ba4 33bb4|
### Hints and Guidelines
We can split the solution of the problem into 3 parts:
* **Reading the input** in the current problem, this includes reading two numbers **`n`** and **`l`**, each on a separate line.
* **Processing The Input Data** using nested loops to iterate through every possible symbol for each of the five password symbols.
* **Printing the output** printing every "dumb" password that meets the requirements.
#### Reading and Processing The Input Data
For **reading** of **input** data, we will declare two integer variables **`int`**: **`n`** and **`l`**.
![](/assets/chapter-7-2-images/01.stupid-password-generator-1.png)
#### Printing Output
One of the ways to find a solution for this problem is to create **five** **`for`** nested loops, one for each variable. To ensure that the last digit is **greater** than the first two, we will use the built-in method **`max(…)`**:
![](/assets/chapter-7-2-images/01.stupid-password-generator-2.png)
**Do you know that…?**
* For convenience we can use the ASCII value of the symbol **"a"** directly in the declarations of the loops - **`97`**:
![](/assets/chapter-7-2-images/01.stupid-password-generator-3.png)
* If we are not sure about a given value of a symbol or we want to take the values of dynamically generated symbols, we can use the built-in function **`ord(char)`**, which returns the integer representation of the character:
![](/assets/chapter-7-2-images/01.stupid-password-generator-4.png)
* Or the opposite, we can generate a character from a given integer, using a built-in function in Python **`chr(number)`**:
![](/assets/chapter-7-2-images/01.stupid-password-generator-5.png)
* **Python** has a wide range of built-in functions and methods for working with strings. Look for more information on the following functions: **`str(number)`**, **`.lower()`**, **`.format(*args, **kwargs)`**, **`.swapcase()`**, **`.upper()`**.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1058#0](https://judge.softuni.org/Contests/Practice/Index/1058#0).
## Problem: Magic Numbers
Write a program that enters a single integer **magic** number and produces all possible **6-digit numbers** for which **the product of their digits is equal to the magical number**.
Example: "Magic number" &rarr; 2
- 111112 &rarr; 1 \* 1 \* 1 \* 1 \* 1 \* 2 = 2
- 111121 &rarr; 1 \* 1 \* 1 \* 1 \* 2 \* 1 = 2
- 111211 &rarr; 1 \* 1 \* 1 \* 2 \* 1 \* 1 = 2
- 112111 &rarr; 1 \* 1 \* 2 \* 1 \* 1 \* 1 = 2
- 121111 &rarr; 1 \* 2 \* 1 \* 1 \* 1 \* 1 = 2
- 211111 &rarr; 2 \* 1 \* 1 \* 1 \* 1 \* 1 = 2
### Input Data
The input is from the console and consists of **one integer** within the range [**1 … 600 000**].
### Output Data
Print on the console **all magic numbers**, separated by **space**.
### Sample Input and Output
|Input|Output|Input|Output|Input|Output|
|---|---|---|---|---|---|
|2|111112 111121 111211 112111 121111 211111|8|111118 111124 111142 111181 111214 111222 111241 111412 111421 111811 112114 112122 112141 112212 112221 112411 114112 114121 114211 118111 121114 121122 121141 121212 121221 121411 122112 122121 122211 124111 141112 141121 141211 142111 181111 211114 211122 211141 211212 211221 211411 212112 212121 212211 214111 221112 221121 221211 222111 241111 411112 411121 411211 412111 421111 811111|531441|999999|
### Hints and Guidelines
**The solution** of this problem follows the **same** conception (we have to generate all n combinations of the element). Follow the steps and try to solve the problem on your own:
- Declare and initialize an integer type **variable** **`int`** and read the **input** from the console.
- Nest **six `for` loops** one for each digit.
- In the last loop, using an **`if`** statement checks if the **product** of the six digits is **equal** to the **magic** number.
Here is an example implementation of the idea:
![](/assets/chapter-7-2-images/02.magic-numbers-1.png)
In the previous chapter, we reviewed other loop constructions. Let's look at the sample solution of the same problem using the **`while`** loop. First, we need to store the **input magical number** in a suitable variable:
![](/assets/chapter-7-2-images/02.magic-numbers-2.png)
Then we will start writing **`while`** loops.
- We will initialize the **first digit**: **`a = 1`**.
- We will set a **condition for each** loop: the digit will be less than or equal to 9.
- At the **beginning** of each loop, we set a value of the **next** digit, in this case: **`b = 1`**. In the nested **`for`** loops, we initialize the variables in the inner loops at each increment of the outer ones. We want to do the same here.
- At **the end** of each loop, we will **increase** the digit by one: **`a += 1`**, **`b += 1`**, **`c += 1`** etc.
- In the **innermost** loop, we will make **the check** and if necessary, we will print on the console.
![](/assets/chapter-7-2-images/02.magic-numbers-3.png)
As we can see, we can solve a problem using different types of loops. Of course, each task has its most appropriate choice. To practice each type of loop try to solve each of the following problems with all the learned loops.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1058#1](https://judge.softuni.org/Contests/Practice/Index/1058#1).
## Problem: Stop Number
Write a program that prints on the console all numbers from **N** to **M**, that are **divisible by 2** and **3 without remainder**, in **reversed order**. We will read **one more** "stop" number from the console **S**. If any of the numbers divisible by 2 and 3 **is equal to the stop number, it should not be printed** and the program should end. **Otherwise print all numbers up to N**, that meet the condition.
### Input Data
Read from the console 3 numbers, each on a single line:
* **N** - integer number: **0 &le; N &lt; M**.
* **M** - integer number: **N &lt; M &le; 10000**.
* **S** - integer number: **N &le; S &le; M**.
### Output Data
Print on the console on a single line all numbers, that meet the condition, separated by space.
### Sample Input and Output
|Input|Output|Comments|
|---|---|---|
|1<br>30<br>15|30 24 18 12 6|Numbers from 30 to 1, that are divisible at the same time by 2 and 3 without remainder are: 30, 24, 18, 12, and 6. The number 15 **is not equal** to any, so the sequence **continues**.|
|Input|Output|Comments|
|---|---|---|
|1<br>36<br>12|36 30 24 18|Numbers from 36 to 1, that are divisible at the same time by 2 and 3 without remainder are: 36, 30, 24, 18, 12, and 6. The number 12 **is equal** to the stop number, so **we stop by 18**.|
|Input|Output|
|---|---|
|20<br>1000<br>36|996 990 984 978 972 966 960 954 948 942 936 930 924 918 912 906 900 894 888 882 876 870 864 858 852 846 840 834 828 822 816 810 804 798 792 786 780 774 768 762 756 750 744 738 732 726 720 714 708 702 696 690 684 678 672 666 660 654 648 642 636 630 624 618 612 606 600 594 588 582 576 570 564 558 552 546 540 534 528 522 516 510 504 498 492 486 480 474 468 462 456 450 444 438 432 426 420 414 408 402 396 390 384 378 372 366 360 354 348 342 336 330 324 318 312 306 300 294 288 282 276 270 264 258 252 246 240 234 228 222 216 210 204 198 192 186 180 174 168 162 156 150 144 138 132 126 120 114 108 102 96 90 84 78 72 66 60 54 48 42|
### Hints and Guidelines
The problem can be divided into **four** logical parts:
* **Reading** the input from the console.
* **Checking** all numbers in the given range, and then running a **loop**.
* **Checking** the conditions of the problem according to every number in the given range.
* **Printing** the numbers.
The **first** part is ordinary - we read **three** integer numbers from the console
We have already seen examples of the **second** part initialization of the **`for`** loop. It is a bit **tricky** - the explanation mentions that the numbers have to be printed in **reversed order**. This means that the **initial** value of the variable **`i`** will be **bigger**, and from the examples, we can see that it is **M**. Thus, the **final** value of **`i`** should be **N**. The fact that we will print the results in reversed order and the values of **`i`** suggest that the step would be **decreased by 1**:
![](/assets/chapter-7-2-images/03.stop-number-1.png)
After we have initialized the **`for`** loop, it is time for the **third** part of the problem - **checking** the condition if the given **number is divisible both by 2 and 3 without remainder**. We will do this using one simple **`if`** condition that we will leave to the reader to do by themselves.
Another **tricky** part of this problem is that apart from the above check we need to do **another** one whether the **number is equal to the "stop" number**, entered from the console on the third line. To do this check, the previous one has to be passed. For this reason, we will add another **`if`** statement that we will **nest in the previous one**. If the condition is **true**, we need to stop the program from printing. We can do this using a **`break`**, operator, and it will lead us **out** of the **`for`** loop.
If the **condition** that checks whether the number is equal with the "stop" number returns a **`false`**, result, our program should **continue to print**. This covers the **fourth and last** part of our program.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1058#2](https://judge.softuni.org/Contests/Practice/Index/1058#2).
## Problem: Special Numbers
Write a program that **reads one integer number N** and generates all possible **"special numbers** from **1111** to **9999**. To be considered **"special"** a number must correspond to the **following condition**:
- **N to be divisible by each of its digits without remainder**.
**Example:** upon **N = 16, 2418** is a special number:
- 16 / 2 = 8 **without remainder**
- 16 / 4 = 4 **without remainder**
- 16 / 1 = 16 **without remainder**
- 16 / 8 = 2 **without remainder**
### Input Data
The input is read from the program and consists of **one integer** within the range **[1 … 600 000]**.
### Output Data
Print on the console **all special numbers**, separated by **space**.
### Sample Input and Output
|Input|Output|Comments|
|---|---|---|
|3|1111 1113 1131 1133 1311 1313 1331 1333 3111 3113 3131 3133 3311 3313 3331 3333|3 / 1 = 3 without remainder<br>3 / 3 = 1 without remainder<br>3 / 3 = 1 without remainder<br>3 / 3 = 1 without remainder|
|Input|Output|Input|Output|
|---|---|---|---|
|11|1111|16|1111 1112 1114 1118 1121 1122 1124 1128 1141 1142 1144 1148 1181 1182 1184 1188 1211 1212 1214 1218 1221 1222 1224 1228 1241 1242 1244 1248 1281 1282 1284 1288 1411 1412 1414 1418 1421 1422 1424 1428 1441 1442 1444 1448 1481 1482 1484 1488 1811 1812 1814 1818 1821 1822 1824 1828 1841 1842 1844 1848 1881 1882 1884 1888 2111 2112 2114 2118 2121 2122 2124 2128 2141 2142 2144 2148 2181 2182 2184 2188 2211 2212 2214 2218 2221 2222 2224 2228 2241 2242 2244 2248 2281 2282 2284 2288 2411 2412 2414 2418 2421 2422 2424 2428 2441 2442 2444 2448 2481 2482 2484 2488 2811 2812 2814 2818 2821 2822 2824 2828 2841 2842 2844 2848 2881 2882 2884 2888 4111 4112 4114 4118 4121 4122 4124 4128 4141 4142 4144 4148 4181 4182 4184 4188 4211 4212 4214 4218 4221 4222 4224 4228 4241 4242 4244 4248 4281 4282 4284 4288 4411 4412 4414 4418 4421 4422 4424 4428 4441 4442 4444 4448 4481 4482 4484 4488 4811 4812 4814 4818 4821 4822 4824 4828 4841 4842 4844 4848 4881 4882 4884 4888 8111 8112 8114 8118 8121 8122 8124 8128 8141 8142 8144 8148 8181 8182 8184 8188 8211 8212 8214 8218 8221 8222 8224 8228 8241 8242 8244 8248 8281 8282 8284 8288 8411 8412 8414 8418 8421 8422 8424 8428 8441 8442 8444 8448 8481 8482 8484 8488 8811 8812 8814 8818 8821 8822 8824 8828 8841 8842 8844 8848 8881 8882 8884 8888|
### Hints and Guidelines
Solve the problem by yourself using what you learned from the previous two problems. Keep in mind the difference between operators for **integer division `//`** and **division with remainder `%`** in Python.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1058#3](https://judge.softuni.org/Contests/Practice/Index/1058#3).
## Problem: Digits
Write a program that reads from the console 1 integer within the range [**100 … 999**], and then prints it a predefined number of times modifying it before each print, as follows:
- If the number is divisible by **5** without remainder, **subtract** from it **its first digit**.
- If the number is divisible by **3** without remainder, **subtract** from it **its second digit**.
- If none of the above-mentioned conditions is valid, **add** to it **its third digit**.
Print on the console **N lines**, and each line has **M numbers**, that are the result of the above actions. Let:
- N = sum of the first and second digits of the number.
- M = sum of the first and third digits of the number.
### Input Data
The input is read from the console and is **an integer** within the range [**100 … 999**].
### Output Data
Print on the console **all integer numbers**, that is the result of the above-mentioned calculations in the respective number of rows and columns as in the examples.
### Sample Input and Output
|Input|Output|Comments|
|---|---|---|
|132|129 126 123<br>120 119 121<br>123 120 119<br>121 123 120|(1 + 3) = 4 and (1 + 2) = 3 &rarr; 4 lines with 3 numbers in each<br>Input number 132 <br>132 &rarr; division by 3 &rarr; 132 - 3 =<br>= 129 &rarr; division by 3 &rarr; 129 - 3 = <br>= 126 &rarr; division by 3 &rarr; 126 - 3 = <br>= 123 &rarr; division by 3 &rarr; 123 - 3 = <br>= 120 &rarr; division by 5 &rarr; 120 - 1 = <br>..... 121 &rarr; neither by 5, nor 3 &rarr; 121 + 2 = 123|
|Input|Output|Comments|
|---|---|---|
|376|382 388 394 400 397 403 409 415 412<br>418 424 430 427 433 439 445 442 448<br>454 460 457 463 469 475 472 478 484<br>490 487 493 499 505 502 508 514 520<br>517 523 529 535 532 538 544 550 547<br>553 559 565 562 568 574 580 577 583<br>589 595 592 598 604 610 607 613 619<br>625 622 628 634 640 637 643 649 655<br>652 658 664 670 667 673 679 685 682<br>688 694 700 697 703 709 715 712 718|10 lines with 9 numbers in each<br>Input number 376 &rarr; neither 5, nor 3 &rarr; 376 + 6 &rarr; =<br>= 382 &rarr; neither 5, nor 3 &rarr; 382 + 6 =<br>= 388 + 6 = 394 + 6 =<br>400 &rarr; division by 5 &rarr; 400 - 3 = 397|
### Hints and Guidelines
Solve the problem **by yourself**, using what you learned from the previous ones. Remember that you will need to define **different** variables for each digit of the input number.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1058#4](https://judge.softuni.org/Contests/Practice/Index/1058#4).

View File

@@ -0,0 +1,699 @@
# Chapter 7.1. Complex Loops
Since we have learned what **`for` loops** are and their function in code, now is the time to take a look at **other loop types**, as well as some **more complex loop constructs**. These will expand our knowledge and help us solve more challenging problems. In particular, we will discuss how to use the following programming constructs:
* loops **with step**
* **`while`** loops
* **`while`** + **`break`** loops
* **infinite** loops
In the current chapter, we will also take a look at the **`break`** operator and **how** to use it to **interrupt** a loop. We will also learn how to process **errors during the execution** of the program, using the **`try-except`** statement.
## Loops with a Step
In the ["Loops (Repetitions)"](/chapter-05-loops.md) chapter we learned how the **`for`** loop works and we already know when and for what purpose it is used. In the present chapter we will **take a look** at a particular and a very important **part of this structure** - its **step**.
### Explanation
The **step** is **part** of the **'range'** function, which specifies the **amount** used to **increment** or **decrement** the value of the **main** variable. The step is declared as the last argument in the **`range`** function.
By default, range in Python uses a step **value of `1`** and is not added to the **`range`** function. If we want our step to be **different from 1**, when writing the **`range`** function, we add another number as the last parameter, which is our step. With a step of **`10`**, the loop would appear as below:
![](/assets/chapter-7-1-images/00.Step-explanation-01.png)
Here is a series of sample problems, the solution of which will help us better understand the use of a **step** in a **`for`** loop.
### Problem: Numbers 1...N with Step 3
Write a program that prints the numbers **from 1 to n** with a **step of 3**. For example, **if n = 100**, then the output would be: **1, 4, 7, 10, …, 94, 97, 100**.
We can solve the problem using the following sequence of actions (algorithm):
* We read **`n`** from the console.
* We run a **`for` loop** from **1** to **`n`** (including **`n`**) with a step of **3**.
* In the **body of the loop**, we print the value of the current step.
![](/assets/chapter-7-1-images/01.Numbers-1-to-n-01.png)
#### Testing in The Judge System
You can test your solution at the following link: [https://judge.softuni.org/Contests/Practice/Index/1057#0](https://judge.softuni.org/Contests/Practice/Index/1057#0).
### Problem: Numbers N...1
Write a program that prints the numbers **from n to 1 in reverse** (step -1). For example, **if n = 100**, then the output will be: **100, 99, 98, …, 3, 2, 1**.
We can solve the problem in the following manner:
* We read **`n`** from the console.
* We create a **`for` loop**, from **`n`** to 0.
* We define the step size: **-1**.
* In the **body of the loop**, we print the value of the current step.
![](/assets/chapter-7-1-images/02.Numbers-n-to-1-01.png)
#### Testing in The Judge System
You can test your solution at the following link: [https://judge.softuni.org/Contests/Practice/Index/1057#1](https://judge.softuni.org/Contests/Practice/Index/1057#1).
### Problem: Powers of Two
In the following example, we will use the standard size 1 step.
Write a program that prints the numbers **from 1 to 2^n** (two to the power of n). For example, **if n = 10**, then the output would be **1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024**.
![](/assets/chapter-7-1-images/03.Numbers-1-to-2^n-01.png)
#### Testing in The Judge System
You can test your solution at the following link: [https://judge.softuni.org/Contests/Practice/Index/1057#2](https://judge.softuni.org/Contests/Practice/Index/1057#2).
### Problem: Even Powers of 2
Print the **even** powers of **2** until **2^n**: **2^0, 2^2, 2^4, 2^8, …, 2^n**. For example, if **n = 10**, then the output would be **1, 4, 16, 64, 256, 1024**.
Here is how we can solve the problem:
* We declare a variable **`num`** that will hold the current number and we assign it the initial **value of 1**.
* For the **step** of the loop, we set a value of **2**.
* In the **body of the loop**: we print the value of the current number and **increase the current number `num` 4 times** (as per the problem description).
![](/assets/chapter-7-1-images/04.Even^2-01.png)
#### Testing in The Judge System
You can test your solution at the following link: [https://judge.softuni.org/Contests/Practice/Index/1057#3](https://judge.softuni.org/Contests/Practice/Index/1057#3).
## While loop
The next type of loops that we will become familiar are called **`while` loops**. The special thing about them is that they repeat a command block, **while a condition is met**. Their structure is a bit different than that of the **`for`** loops, however, they boast a simpler syntax.
### Explanation
In programming, the **`while` loop** is used when we want to **repeat** the execution of a specific logic block until **a specific condition is met**. Any expression that returns either **`true`** or **`false`** (a Boolean) can be used as a "**condition**". When the **condition** becomes **invalid**, the **`while`** loop **is interrupted** and the program **proceeds** to execute the code after the loop. The **`while` loop** structure looks like this:
![](/assets/chapter-7-1-images/00.While-loop-01.png)
Here is a series of sample problems, the solutions of which will help us better understand the use of the **`while`** loop.
### Problem: Sequence 2k+1
Write a program that prints **all numbers ≤ n** in the series: **1, 3, 7, 15, 31, …**, assuming that each number is generated according to the following formula next_number = **previous_number \* 2 + 1**.
Here is how we can solve the problem:
* We declare a variable **`num`** that will hold the current number and we assign it the initial **value of 1**.
* For loop condition, we use **the current number <= n**.
* In the **body of the loop**: we print the value of the current number and increase the current number by using the formula above.
Here is a sample implementation of this idea:
![](/assets/chapter-7-1-images/05.Numbers-2k+1-01.png)
#### Testing in The Judge System
You can test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#4](https://judge.softuni.org/Contests/Practice/Index/1057#4).
### Problem: Number in Range [1 … 100]
Enter an integer in the range [**1 … 100**]. If the entered number is **invalid**, enter **again**. In this case, an invalid number would be any number that is **outside** the given range.
To solve the problem, we can use the following algorithm:
* We declare a variable **`num`**, to which we assign the integer, received from the console.
* For a loop condition, we put a **`True`** expression, if the number **is not** in the range specified in the problem description.
* In the **body of the loop**: we print the message "**Invalid number!**" on the console, afterwards we assign a new value to **`num`** (the next line from the console input).
* Once we have validated the entered number, we print its value outside the body of the loop.
Here is a sample implementation of the algorithm using a **`while` loop**:
![](/assets/chapter-7-1-images/06.Numbers-in-range-1..100-01.png)
#### Testing in The Judge System
You can test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#5](https://judge.softuni.org/Contests/Practice/Index/1057#5).
## Greatest Common Divisor (GCD)
Before proceeding to the next problem, we should become familiar with the definition of **the greatest common divisor** (**GCD**).
**Definition of GCD**: the greatest common divisor of two **natural** numbers **a** and **b** is the largest number that **divides both** **a** and **b** without reminder. For example:
|a|b|GCD|
|:---:|:---:|:---:|
|24|16|8|
|67|18|1|
|12|24|12|
|15|9|3|
|10|10|10|
|100|88|4|
## The Euclidean Algorithm
In the next problem we will use one of the first published algorithms for finding the GCD - **Euclid's algorithm**:
**Until** we have a remainder of 0:
* We divide the greater number by the smaller one.
* We take the remainder of the division.
Euclid's algorithm **pseudo-code**:
```python
while b 0
oldB = b
b = a % b
a = oldB
print a
```
### Problem: Greatest Common Divisor (GCD)
Enter the **integers** **a** and **b** and find **GCD(a, b)**.
We will solve the problem by implementing **Euclid's algorithm**:
* We declare variables **`a`** and **`b`**, to which we assign the **integer** values, passed by the console input.
* For loop condition, we use a **`True`** expression, if the number **`b`** **is different** than **0**.
* In the **body of the loop** we follow the instructions from the pseudo-code:
* We create a temporary variable to which we assign the **current** value of **`b`**.
* We assign a new value to **`b`**, which is the remainder of the division of **`a`** and **`b`**.
* On variable **`a`**, we assign the **previous** value of variable **`b`**.
* Once the loop is complete and we have found the GCD, we print it on the screen.
![](/assets/chapter-7-1-images/07.GCD-01.png)
#### Testing in The Judge System
You can test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#6](https://judge.softuni.org/Contests/Practice/Index/1057#6).
## While True + break loop
The next type of loop construction that we will become familiar with while studying programming is the `while True` + `break` loop (for short `while` + `break` loop). Its idea **is to repeat a block of code** over and over again until **explicit termination of the loop**, usually after an `if` statement in the body of the loop. Here's what this loop looks like in Python code:
![](/assets/chapter-7-1-images/00.Do-while-01.png)
The above example is called “**inverted `while` loop**”, because the condition for exiting the loop is at the end, not at the beginning. In essence, the above construction is an “infinite loop” with a check, of a given condition, for an exit inside the body of the cycle.
In programming, the `break` operator unconditionally terminates a loop and proceeds to the first instruction after it. In the example above, a condition is checked at the end of the cycle, and if it is not true, the cycle is terminated.
The construct for the `while` + `break` loop in many other programming languages is implemented with the `do-while` construct, but there is no direct equivalent of `do-while` in Python. To do the same in Python, we can use an infinite loop (`while True`) and when an exit condition is reached, the loop is interrupted (with `break`).
The `while` + `break` construct provides more flexibility than `while` loops because it allows to break the loop at **any place** in the body of the loop(for example at the beginning, in the middle, or at the end), it even allows to have exit of the loop in several different places (with several `break` operators).
The structure of `while` + `break` loop is very similar to the classic `while` loop, but there is a significant difference: `while` is executed 0 or more times (according to the entry condition of the loop), whilst `while` + `break` will execute its body **at least once**. Why is this? In the `while True` + `break` loop structure, the **condition** is always checked **in the body of the loop**, whilst with the classic `while` loop the condition, which we check to exit the loop is always at the beginning, before its body.
Once we are familiar with the concept of a `while` + `break` loop with an exit condition, which is not required to be at the beginning, now we should proceed with the usual set of practical problems, with which we can master the new knowledge.
### Problem: Factorial
For **n** number calculate **n! = 1 \* 2 \* 3 \* … \* n**. For example, if **n = 5**, then the result would be: **5!** = 1 \* 2 \* 3 \* 4 \* 5 = **120**.
Here is how we can calculate factorial in more detail:
* We declare the variable **`n`**, to which we assign the integer value, received from the input of the console.
* We declare another variable - **`fact`**, with an initial value of 1. We will use it in the calculation and store the factorial value.
* For the loop condition we will use **`n > 1`**, because each time we perform the calculations in the body of the loop, we will decrease the value of **`n`** by 1.
* In the body of the loop:
* We assign a new value to the **`fact`**, which value is the product of multiplying the current **`fact`** with the current **`n`**.
* We decrement **`n`** with **1**.
* Outside the body of the loop, we print the final factorial value.
![](/assets/chapter-7-1-images/08.Factorial-01.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#7](https://judge.softuni.org/Contests/Practice/Index/1057#7).
### Problem: Sum Digits
Sum up the digits of the integer **positive** number **n**. For example, if **n = 5634**, then the output would be: 5 + 6 + 3 + 4 = **18**.
We can use the following idea to solve the problem:
* We declare the variable **`n`**, to which we assign a value equal to the number received by the user.
* We create a second variable - **`sum`**, with an initial value of 0. We will use it for the calculation and storage of the result.
* As a loop condition, we will use **`n > 0`**, since, after each iteration of the loop, we will be removing the last digit from **`n`**.
* In the body of the loop:
* We assign a new value to **`sum`**, which is the result of the sum of the current value of **`sum`** with the last digit of **`n`**.
* We assign a new value to **`n`**, which is the result of removing the last digit of **`n`**.
* outside the body of the loop, we print the final value of the sum.
![](/assets/chapter-7-1-images/09.Sum-digits-01.png)
<table><tr><td><img src="assets/alert-icon.png" style="max-width:50px;" /></td>
<td><code><strong>n % 10</strong></code>: <b>returns</b> the last digit of the number <code><strong>n</strong></code>.<br>
<code><strong>n // 10</strong></code>: <b>deletes</b> the last digit of <code><strong>n</strong></code>.</td>
</tr></table>
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#8](https://judge.softuni.org/Contests/Practice/Index/1057#8).
## Infinite Loops and The `break` Operator
So far, we were introduced to various types of loops, learning what structures they have and how they are applied. Now, we need to understand what an **infinite loop** is, when it occurs, and how we can **break** it using the **`break`** operator.
### Infinite Loop Explanation
An **infinite loop** **runs infinitely** the code of its body. With the infinite **`while`** loops, the end check is a conditional expression that **always** returns **`true`**. Here is an example of an **infinite `while`** loop:
![](/assets/chapter-7-1-images/00.Infinite-while-loop-01.png)
### The `break` Operator
We already know that the infinite loop executes a certain code infinitely, but what if we want at some point under a given condition to interrupt and exit the loop? The **`break`** operator comes in handy in this situation.
<table><tr><td><img src="assets/alert-icon.png" style="max-width:50px" /></td>
<td>The operator <b><code>break</code></b> stops a loop's execution at the point it is called and the execution continues from the first line after the end of the loop. This means that the current iteration of the loop will not be completed accordingly and the rest of the code in the body of the loop will not be executed.</td>
</tr></table>
### Problem: Check Prime
The next problem we are going to solve is to **check whether a given number is prime**, but before that, we should remember what prime numbers are.
**Definition**: An integer is considered **prime** if it is divisible only by itself and by 1. By definition, the prime numbers are positive and greater than 1. The smallest prime number is **2**.
We can assume that an integer **n** is a prime number if **n > 1** and **n** is not divisible by a number between **2** and **n-1**.
The first few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, …
By contrast, **composite numbers** are integers, which can be obtained by multiplying several prime numbers.
Here are some examples of composite numbers:
* **10** = 2 \* 5
* **42** = 2 \* 3 \* 7
* **143** = 13 \* 11
**An algorithm to check** whether a given number is **prime**: we check if **n > 1** and if **n** is divisible by **2**, **3**, …, **n-1** without remainder.
* If it is divisible by any of the numbers, it is **composite**.
* If it is not divisible by any of the numbers, then it is **prime**.
<table><tr><td><img src="assets/alert-icon.png" style="max-width:50px" /></td>
<td>We can optimize the algorithm by instead of checking until <code><strong>n-1</strong></code>, to check the divisors only until <code><strong>√n</strong></code>. Think of the reasons why this is so.</td>
</tr></table>
### Problem: Enter Even Number
You are tasked to write a function that takes a single input **n** integer and checks if it is prime. This can be implemented by checking if **n** is divisible by any numbers in the range between 2 and √n.
The steps of the **"prime checking algorithm"** are given below in bigger detail:
* We declare the variable **`n`**, to which we assign the integer passed by the console.
* We create a **`is_prime`** boolean with an initial value of **`True`**. We assume that a number is prime until proven otherwise.
* We create a **`for`** loop, with the initial value set to 2, for a condition the **current value `<= √n`**. The step is set to 1.
* In the **body of the loop**, we check if **`n`**, divided by the **current value** has a remainder. If there is **no reminder** from the division, then we change **`is_prime`** to **`False`** and exit the loop through the **`break`** operator.
* Depending on the value of **`is_prime`** we print whether the input number is prime (**`True`**) or composite (**`False`**).
Here is a sample implementation of the prime checking algorithm, described above:
![](/assets/chapter-7-1-images/10.Check-if-prime-01.png)
What remains is to add a **condition that checks if the input number is greater than 1**, because, by definition numbers such as 0, 1, -1, and -2 are not prime.
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#9](https://judge.softuni.org/Contests/Practice/Index/1057#9).
### The `break` Operator in an Infinite Loop
Write a function, which checks whether a given number **n** is even and if so - print it on the console. An even number can be divided by 2 without a remainder. If the number is invalid, we will print that the current number is not even and the user will need to input a new number.
Here is an idea for the solution:
* We declare a variable **`n`**, with an initial value of **0**.
* We create an infinite **`while`** loop, with a condition set to **`True`**.
* In **the body of the loop**:
* We take the integer value, passed to our function, and assign it to **`n`**.
* If **the number is even**, we exit the loop by a **`break`**.
* **Otherwise**, we print a message stating that **the number is not even**. Iterations continue until an even number is entered.
* We print the even number on the console.
Here is an example implementation of the idea:
![](/assets/chapter-7-1-images/00.Break-in-infinite-loop-01.png)
Note: although the code above is correct, it will not work if the user enters text instead of a number, for example "**Invalid number**". Then the parse of the text to a number will break and the program will display an **error message (exception)**. We will learn very soon how to deal with this problem and how to catch and handle exceptions using the **`try-except` construct**.
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#10](https://judge.softuni.org/Contests/Practice/Index/1057#10).
## Nested Loops and The `break` Operator
Now since we know what **nested loops** are and how the **`break`** operator works, it is time to figure out how they work together. To get a better idea, we should write a **function** step by step, that should make all possible combinations of **number pairs**. The first number in the pair is increasing from 1 to 3, while the second one is decreasing from 3 to 1. Our solution must continue running until **`i + j`** **is not** equal to 2 (i.e. **`i = 1`** and **`j = 1`**).
The desired result is:
![](/assets/chapter-7-1-images/00.Nested-and-break-desired-result-01.png)
Here is a **wrong implementation**, that looks right at first glance:
![](/assets/chapter-7-1-images/00.Nested-and-break-desired-result-02.png)
If we leave our function that way, our result will be as follows:
![](/assets/chapter-7-1-images/00.Nested-and-break-undesired-result-01.png)
Why is this so? As we can see, the result is **missing "1 1"**. When the function reaches the point when **`i = 1`** and **`j = 1`**, it enters the **`if`** check and executes the **`break`** operation. This way, it **exits the inner loop**, but then continues the execution of the **outer loop**. **`i`** increases, the function enters the inner loop and prints the result.
<table><tr><td><img src="/assets/alert-icon.png" style="max-width:50px" /></td>
<td> When we use the <b><code>break</code></b> <b>operator</b> in a <b>nested loop</b>, it interrupts <b>only</b> the execution of the inner loop.</td>
</tr></table>
What is the **correct solution**? One way to solve the problem is by declaring a **`bool` variable**, to keep track if the loop iteration has to continue. If we have to exit (leave all nested loops), we set the variable to **`True`** and exit the inner loop with a **`break`**, and in the next check, we exit the outer loop. Here is an example implementation of this idea:
![](/assets/chapter-7-1-images/00.Nested-and-break-undesired-result-02.png)
In this manner, when **`i + j = 2`**, the program will set the variable **`has_to_end = True`** and will exit the inner loop. Upon the next iteration of the outer loop, via the **`if`** check, the function will not reach the inner loop and will interrupt its execution.
### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#11](https://judge.softuni.org/Contests/Practice/Index/1057#11).
## Dealing with Exceptions: `try-except`
The last thing we'll look at in this chapter is how to "catch" **errors (exceptions)** using the **`try-except`** construction.
### What is `try-except`?
The **`try-except`** construction is used for **catching and handling exceptions (errors)** during program execution.
In programming, **exceptions** are notifications of an event that disrupts the normal operation of a program. Such events **interrupt the execution** of the program, which starts looking for someone to handle the situation. If it is not found, the exception is printed on the console (i.e. the program stops). If found, **the exception is processed** and the program continues normal execution. In a bit, we will see exactly how this happens.
### The `try-except` Construction
The **`try-except`** construction has different options, but for now, we will get acquainted only with the most basic of them
![](/assets/chapter-7-1-images/00.Try...except-01.png)
In the next task, we will see how to deal with a situation, in which the user enters an input other than a number (for example **`string`** instead of **`int`**), using **`try-except`**.
### Problem: Break Sum
Write a program that checks that a number **n** is even and if so, print it on the screen. In case of an **invalid entered** number, a message should be displayed that the entered input is not a valid number and the input should continue.
Here's how we can solve the problem:
* We create an infinite **`while`** loop, as a condition we will set **`True`**.
* In the body of the loop:
* We create a **`try-except`** construction.
* In the block **`try`** we write the program logic for reading the user input, its parsing to a number, and the parity check.
* At an **even number**, we print it and leave the loop (with **`break`**). The program has done its job and is coming to an end.
* At an **odd number**, we print a message that an even number is required without leaving the loop (because we want it to be repeated).
* If we **catch an exception** during the execution of the **`try`** block, we print a message for an invalid entered number (and the loop repeats, because we do not explicitly leave it).
Here is an example implementation of the described idea:
![](/assets/chapter-7-1-images/11.Wrong-numbers-try-except-01.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#10](https://judge.softuni.org/Contests/Practice/Index/1057#10).
Now the solution must always work: whether we enter integers, invalid numbers (for example, numbers with too many digits), or text that does not contain numbers.
## Problems with Loops
In this chapter, we got familiar with a few new types of loops that can perform repetitions with more complex programming logic. Let's solve a few practical problems using these new constructs.
### Problem: Fibonacci
Fibonacci's numbers in mathematics form a sequence that looks like this: **1, 1, 2, 3, 5, 8, 13, 21, 34, …**.
**The formula** to derive the next member of Fibonacci's sequence is:
```python
F0 = 1
F1 = 1
Fn = Fn-1 + Fn-2
```
#### Sample Input and Output
|Input (n)|Output|Comment|
|----|-----|---------|
|10|89|F(11) = F(9) + F(8)|
|5|8|F(5) = F(4) + F(3)|
|20|10946|F(20) = F(19) + F(18)|
|0|1| |
|1|1| |
Enter an **integer** number **n** and calculate the **n-th Fibonacci number**.
#### Hints and Guidelines
An idea to solve the problem:
* We declare a **variable `n`**, which will hold the integer value received from the console input.
* We create the variables **`f0`** and **`f1`**, to which we assign the value to **1** since this is the start of Fibonacci's sequence.
* We create a **`for`** loop starting from 0 to the **final value `n - 1`**.
* In the **body of the loop:**
* We create a **temporary** variable **`f_next`**, to which we assign the next number in the Fibonacci sequence.
* To **`f0`** we assign the current value of **`f1`**.
* To **`f1`** we assign the value of the temporary variable **`f_next`**.
* Out of the loop we print the n-th number of Fibonacci.
Example implementation:
![](/assets/chapter-7-1-images/12.Fibonacci-01.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#12](https://judge.softuni.org/Contests/Practice/Index/1057#12).
### Problem: Number Pyramid
Print the **numbers 1 … n in a pyramid** as per the below example. On the first row, we print one number, at the second we print two, at the third, we print three, and so on, until the numbers are over. On the last line, we print as many numbers as we get until we get to **n**.
#### Sample Input and Output
|Input|Output |Input|Output |Input |Output |
|-----|----------------------|-----|---------------|----------|------------------------------|
|7 |1<br>2 3<br>4 5 6<br>7|5 |1<br>2 3<br>4 5|10 |1<br>2 3<br>4 5 6<br>7 8 9 10 |
#### Hints and Guidelines
We can solve the problem with **two nested loops** (by rows and columns) by printing in them and leaving when the last number is reached. Here is the idea, written in more detail:
* We declare a variable **`n`**, to which we assign the integer value, received from the console input.
* We declare a variable **`num`** with an initial value of 1. It will hold the count of printed numbers. At each iteration, we will **increment** it by **1** and will add it to the current row.
* We create an **outer** **`for`** loop, which will be responsible for the **rows** in the table. The loop variable will be named **`row`** and we assign it an initial value of 1. For condition, we set **`n + 1`**.
* In the body of the loop, we create an **inner** **`for`** loop, which will be responsible for the **columns** in the table. We name the variable **`col`** and assign it an initial value of 1. For condition we set **`row + 1`** (**`row`** = number of digits per line).
* In the body of the nested loop:
* We check whether **`col > 1`**, if true we print space. If we do not make this check and instead print the space every time, each resulting line will start with a space.
* We **print** the number **`num`** in the current cell of the table and **increment it by 1**.
* We check if **`num > n`**. If the **`num`** is greater than **`n`**, we **break** the **inner loop**.
* We print an **empty line**, to move on to the next.
* Again, we check if **`num > n`**. If it is greater, we **interrupt** the execution of **our program** with a **`break`** operator.
Here is an example implementation:
![](/assets/chapter-7-1-images/13.Pyramid-01.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#13](https://judge.softuni.org/Contests/Practice/Index/1057#13).
### Problem: Number Table
Print the numbers 1 … n in a table as per the examples below:
#### Sample Input and Output
|Input|Output|Input|Output|
|--------|-----|-------|-----|
|3|1 2 3<br>2 3 2<br>3 2 1|4|1 2 3 4<br>2 3 4 3<br>3 4 3 2<br>4 3 2 1|
#### Hints and Guidelines
We can solve the problem by using **two nested loops** and some simple calculations in them:
* We take the size of the table from the integer value of the variable **`n`**, which is received from the console input.
* We create a **`for`** loop, that will be responsible for the rows of the table. We name the loop variable **`row`** and assign it an initial **value of 0**. For condition, we set **`n`**. The size of the step is 1.
* In **the body of the loop** we create a nested **`for`** loop, that will be responsible for the columns in the table. We name the loop variable **`col`** and assign it an initial **value of 0**. For condition, we set **`n`**. The size of the step is set to 1.
* In **the body of the nested loop**:
* We create a variable **`num`**, to which we assign the result of **the current row + the current column + 1** (+1, is needed since we count from 0).
* We check whether **`num > n`**. If **`num`** is **greater** than **`n`**, we assign **`num`** a new value equal to **two times `n`** - the current value of **`num`**. We do this in order not to exceed **`n`** in any of the cells of the table.
* We print the number from the current table cell.
* We print an **empty line** in the outer loop, to move on to the next line.
Here is an example implementation:
![](/assets/chapter-7-1-images/14.Table-with-numbers-01.png)
#### Testing in The Judge System
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/1057#14](https://judge.softuni.org/Contests/Practice/Index/1057#14).
## What Have We Learned from This Chapter?
We can use **`for`** loops with a **step**:
```python
for i in range(1, n + 1, 3):
print(i)
```
The **`while`** loop is repeated while a condition **is True**:
```python
num = 1
while num <= n:
print(num)
num += 1
```
If we have to **interrupt** the loop execution, we use the **`break`** operator:
```python
n = 0
while True:
n = int(input())
if n % 2 == 0:
break # even number -> exit from the loop
print("The number is not even.")
print("Even number entered: {}".format(n))
```
We already know how to catch **errors** during the execution of our program:
```python
try:
print("Enter even number: ", end="")
n = int(input())
except ValueError:
print("Invalid number.")
# If int(…) gets an error, it will complete the exception block of code
```
## Problem: Web Applications with More Complex Loops
Now we know how to repeat a group of actions using **loops**. Let's do something interesting: a **web-based game**. Yes, a real game, with graphics and game logic. Let's have fun. It will be complicated, but if you do not understand exactly how it works, that is no problem. We are still just entering programming. There is time, you will advance with this technology. For now, follow the steps.
### Problem: Web Game "Shoot The Fruits!"
**Condition**: Develop a **Flask web application** a game in which the player **shoots at fruits**, arranged in a table. Successfully hit fruits disappear, and the player receives points for each fruit that was hit. The shooting is performed on columns, from top to bottom or vice-versa, and the location of the hit (the column under fire) is set by a scroll bar. Due to the inaccuracy of the scroller, the player is not quite sure which column to shoot at. So with each shot, there is a chance not to hit and this makes the game more interesting (similar to the slingshot in Angry Birds).
Our game should look like this:
![](/assets/chapter-7-1-images/15.Fruits-01.png)
![](/assets/chapter-7-1-images/15.Fruits-02.png)
Shown below, are the steps for implementing the "Shoot the Fruits!" Web application.
### Empty PyCharm Solution
We create an empty solution in PyCharm to organize the code from the application:
![](/assets/chapter-7-1-images/15.Blank-Python-project-01.png)
Then, we give a meaningful name to the project, for example "**Fruits-Web-Game**". Also, set the Python interpreter to the default one:
![](/assets/chapter-7-1-images/15.Blank-Python-project-02.png)
We will create our web application using the **Flask** library, which we have already met. As we already know, before we start writing code, we need to install it. We go to PyCharm settings [**File**] -> [**Settings**] and then to [**Project: Fruit-Web-Game**] -> [**Project Interpreter**]. There, press the **`+`** button, search for and install **`Flask`**.
Now we need to take the structure of the project from the resources provided to us.
Then, we add the **resources** for the game (they are part of the task files for this project and can be downloaded from [here](https://github.com/SoftUni/Programming-Basics-Book-CSharp-Python/tree/master/assets/chapter-7-1-assets)). We copy them from Windows Explorer and paste them into the project folder in PyCharm with **copy/paste**. Once we have placed the resources, the structure of the project should look like this:
![](/assets/chapter-7-1-images/15.Fruits-06.png)
If we open **app.py** and launch it with the right button -> [**Run 'app'**], we should open the application, then click on the link in the console and open our web browser :
![](/assets/chapter-7-1-images/15.Fruits-07.png)
Now we create the controls for the game. The goal is to add **scroll bars**, with which the player can aim, and a button to start a **new game**. Therefore, we need to edit the **templates/index.html** file. Delete "**Hello World**" and enter the following code in its place:
![](/assets/chapter-7-1-images/15.Fruits-08.png)
This code creates a web form **`<form>`** with a scroller (field) **`position`** to set a number in the interval [**0 … 100**] and a button [**Fire Top**] to send the form data to the server. The action that will process the data is called **`/FireTop`**, which means the **`fire_top()`** function, which is located in the **app.py** file. There are two more similar forms with buttons [**Fire Bottom**] and [**New Game**].
Now we need to prepare the fruits for drawing in the view. Add the following code to the **app.py** file:
![](/assets/chapter-7-1-images/15.Fruits-09.png)
The above code defines the fields for a **number of rows**, a **number of columns**, for **fruit table** (playing field), for the **points** accumulated by the player, and information whether the game is active or **finished** (field **`gameOver`**). The playing field is 9 columns in 3 rows and contains the text for each field: **`apple`, `banana`, `orange`, `kiwi`, `empty`, or `dynamite`**. The main action **`index()`** prepares the playing field for drawing by saving the elements of the game and calling the view that draws them on the game page (in the web browser as HTML).
We need to generate random fruits. To do this, we need to write the method **`generate_random_fruits()`** with the code from the picture below. This code writes in the table (matrix) **`fruits`** names of different pictures and thus builds the playing field. One of the following values is written in each cell of the table: **`apple`, `banana`, `orange`, `kiwi`, `empty`, or `dynamite`**. Then, to draw the corresponding image in the view, **`.png`** will be pasted to the text from the table and thus the name of the image file will be obtained, which will be inserted into the HTML page as part of the playing field. Filling the playing field (9 columns with 3 rows) is done in the view **`index.html`**, with two nested **`for`** loops (per row and column).
To generate random fruits, a **random number** between 0 and 8 is generated for each cell (see class **`random`** in Python). If the number is 0 or 1, put **`apple`**, if it is between 2 and 3, put **`banana`**, etc. If the number is 8, set **`dynamite`**. Thus, fruits appear 2 times more often than dynamite. Here is the code:
![](/assets/chapter-7-1-images/15.Fruits-10.png)
Drawing the fruit in **`index.html`**:
To **fill the playing field** with the fruits, we have to iterate **two nested loops** (for the rows and the columns). Each line consists of 9 pictures, each of which contains an **`apple`, `banana`**, or another fruit, or an empty field **`empty`**, or **`dynamite`**. Images are drawn by printing an HTML tag to insert an image: **`<img src="/images/apple.png" />`**. Nine pictures are arranged one after the other on each of the lines, and then they are moved to a new line with **`<br>`**. This is repeated three times for the three lines. Finally, the player's points are printed. Here's how the **code** for drawing the playing field and points looks like:
![](/assets/chapter-7-1-images/15.Fruits-11.png)
Note the curly braces they are used to switch between the languages - **HTML** and **Python** and come from **Jinja2** syntax for drawing dynamic web pages.
We start the project with [**Shift+F10**]. It is expected that a random fruit field with sizes 9 by 3 will be generated and visualized on the web page through a series of pictures:
![](/assets/chapter-7-1-images/15.Fruits-12.png)
Now the game is somewhat done: the playing field is randomly generated and visualized successfully (if you haven't made a mistake somewhere). It remains to realize the essence of the game: **shooting at the fruits**.
To do this, add the actions [**New Game**], [**Fire Top**], and [**Fire Bottom**] to the file **app.py**:
![](/assets/chapter-7-1-images/15.Fruits-13.png)
Using the code above we define three actions:
* **`reset()`** launches a new game by generating a new random playing field with fruits and explosives, resets the player's points, and makes the game valid **(`gameOver = false`)**. This action is quite simple and can be tested immediately with [**Shift+F10**] before writing the others.
* **`fire_top()`** shoots in line **0** at **`position`** (number from 0 to 100), taken by the user. A **down** (+1) shooting from row **0** (top) is called. The shooting itself is more complex in logic and will be discussed shortly.
* **`fire_bottom()`** shoots in line **2** at **`position`** (number from 0 to 100), taken by the user. A shoot in the **up** (-1) direction from row **2** (bottom) is called.
We implement the "shooting" method **`fire(position, start_row, step)`**:
![](/assets/chapter-7-1-images/15.Fruits-14.png)
Shooting works as follows: first, the number of the column **`col`**, to which the player is aiming is calculated. The input number from the scroller (between 0 and 100) s reduced to a number between 0 and 8 (for each of the 9 columns). The line number **row** is either 0 (if the shot is at the top) or the number of lines minus one (if the shot is at the bottom). Accordingly, the direction of firing (the step) is **1** (down) or **-1** (up).
To find where the shot hits a fruit or dynamite, go through a loop through all the cells of the playing field in the target column and from the first to the last attacked row. If the fruit is found, it disappears (replaced by **`empty`**) and points are given to the player. If **`dynamite`** is encountered, the game is marked as over.
We leave it to more avid readers to implement more complex behaviors, for example, to give different points when hitting a different fruit, to realize an animation with an explosion (this is not too easy), to take away points when shooting unnecessarily in an empty column, and similar.
**We test** what works so far by running the application with [**Ctrl + Shift + F10**]:
* **New game** ; the new game button must generate a new playing field with randomly placed fruits and explosives and reset the player's points.
* **Shooting from above** ; shooting from above must remove the top fruit in the hit column or cause the end of the game in dynamite. In fact, at the end of the game, nothing will happen yet, because in the view this case is not yet considered.
* **Shooting from below** ; shooting from below must remove the bottom fruit in the hit column or stop the game when hitting dynamite.
![](/assets/chapter-7-1-images/15.Fruits-01.png)
For now, nothing is happening at **"End of Game"**.If the player hits dynamite, the application notes that the game is over **(`game_over = True`)**, but this fact is not visualized in any way. For the game to end, we need to add a few checks to the view:
![](/assets/chapter-7-1-images/15.Fruits-15.png)
The code above checks if the game is over and shows respectively the shooting controls and the playing field (during active play) or a picture with exploded fruits at the end of the game.
After changing the code of the view, we start with [**Ctrl + Shift + F10**] and **test** the game again:
![](/assets/chapter-7-1-images/15.Fruits-16.png)
This time when hitting dynamite, the right picture should appear and only the "new game" action should be allowed (the [**New Game**] button).
Was it complicated? Did you manage to make the game? If you fail, don't worry, this is a relatively complex project that involves a large amount of unstudied material. If you encounter any difficulties, you can ask in the **Softuni's Reddit Community**: [https://www.reddit.com/r/softuni/](https://www.reddit.com/r/softuni/).