JCA Python - Junior v. 2021 Lesson 10 Foxit
JCA Python - Junior v. 2021 Lesson 10 Foxit
LOOPS.
THE WHILE LOOP.
“GUESS
THE NUMBER”
GAME
Contents
Loops..................................................................................3
The while Loop................................................................ 4
“Guess the Number” Game.......................................... 11
This lesson uses videos that have a PLAY icon over the
illustration. Click on it to watch the video.
For correct displaying the videos, we recommend opening the
lesson in the Foxit PDF Reader.
2
Loops. The while Loop. “Guess the Number” Game
Loops
People often have to solve the same tasks and repeat
monotonous actions.
Therefore, they try to automate and delegate such tasks
to machines that are better at it.
By the way, this is one of the main tasks of program-
mers, who do not like to repeat themselves in their work.
Therefore, to re-execute a block of code, they have devel-
oped specific constructions, — loops, in that a sequence of
actions is written only once.
At the same time, the loops are different, and they are
conventionally divided into:
■■ the loops with a known number of repetitions (with a
parameter). This type is characterized by the fact that the
specified sequence of actions is performed the specified
number of times. For example, there is a task: to pull up
20 times. Then the body of the cycle, which describes one
pull-up, will be performed 20 times.
■■ the loops with an unknown number of repetitions (with
a condition). For this loop, repeating will continue until
the condition to exit the loop is executed. Returning to the
pull-ups, this task will continue until the fatigue sets in.
But note that any loop can be turned into a loop with a
parameter or a conditional loop.
3
Lesson 10
while [condition]:
[code]
var_1 = 0
while var_1 < 5:
var_1+=1
print(var_1)
Video 1
4
Loops. The while Loop. “Guess the Number” Game
Let’s run the code and get the result shown in Video 2 on
page 6.
5
Lesson 10
Video 2
i=0
list_1 = ["Macbook", "Iphone", "Tesla", "Space X",
"noodle"]
while i < len(list_1):
list_1[i]= list_1[i]+" - "+ input(list_1[i]+" ")
i+=1
print(list_1)
i=0
while i < 5:
print("infinity ")
Video 3
Video 4
i=0
while i < 5:
print("infinity")
break
7
Lesson 10
Video 5
i=0
list_1 = ["Macbook", "Iphone", "Tesla", "Space X",
"noodle"]
while 1:
print(list_1[i])
if list_1[i] == "Tesla":
print("Hooray! Tesla!")
break
i+=1
8
Loops. The while Loop. “Guess the Number” Game
Video 6
i=0
list_1 = ["Macbook", "Iphone", "Tesla", "Space X",
"noodle"]
while 1:
print(list_1[i])
if list_1[i] == "Tesla":
print("Hooray! Tesla!")
break
if i == len(list_1)-1:
print("The fuse went off")
break
i+=1
9
Lesson 10
Video 7
Video 8
10
Loops. The while Loop. “Guess the Number” Game
As you can see that the “preventer” has worked and saved
the program, so do not forget to add it while working with
the infinite loop.
Figure 1
11
Lesson 10
import random
print("-------Guess my number-------")
print("You need to guess the number picked by
enigmatic computer!")
print("Number is in range from 1 to 10")
magic_number = random.randint(1, 10)
count = 0
12
Loops. The while Loop. “Guess the Number” Game
user_number = input()
import random
print("-------Guess my number-------")
print("You need to guess the number picked by
enigmatic computer!")
print("Number is in range from 1 to 10")
magic_number = random.randint(1, 10)
count = 0
user_number = int(input())
import random
print("-------Guess my number-------")
print("You need to guess the number picked by
enigmatic computer!")
print("Number is in range from 1 to 10")
magic_number = random.randint(1, 10)
count = 0
user_number = int(input())
while user_number != magic_number:
13
Lesson 10
Now it turns out that the user will enter a value once,
and if he doesn’t guess it, the loop will become an infinite
one. It is necessary to implement the input of a number in-
side the loop, but if you move the variable there, an error will
occur, because the condition will be checked before the vari-
able creation. But there is a way out, — to declare a variable,
and assign it a zero value before the loop. And after then, the
user will input the values in the loop itself:
import random
print("-------Guess my number-------")
print("You need to guess the number picked by
enigmatic computer!")
print("Number is in range from 1 to 10")
magic_number = random.randint(1, 10)
count = 0
user_number = 0
while user_number != magic_number:
user_number = int(input("Your number: "))
count+=1
import random
print("-------Guess my number-------")
14
Loops. The while Loop. “Guess the Number” Game
import random
print("-------Guess my number-------")
print("You need to guess the number picked by
enigmatic computer!")
print("Number is in range from 1 to 10")
magic_number = random.randint(1, 10)
count = 0
15
Lesson 10
user_number = 0
while user_number != magic_number:
user_number = int(input("Your number: "))
count+=1
if magic_number > user_number:
print("The magic number is greater than
yours!")
elif magic_number < user_number:
print("The magic number is less than
yours!")
print("You Win! You guessed it on the", count,
"try")
Video 9
16
Loops. The while Loop. “Guess the Number” Game
17
Lesson 10
LOOPS. THE WHILE LOOP.
“GUESS THE NUMBER” GAME
© STEP IT Academy
www.itstep.org
All rights to protected pictures, audio, and video belong to their authors or legal owners.
Fragments of works are used exclusively in illustration purposes to the extent justified by
the purpose as part of an educational process and for educational purposes in accordance
with Act of “On Copyright and Related Rights.” The extent and method of cited works are in
conformity with the standards, do not conflict with a normal exploitation of the work, and
do not prejudice the legitimate interests of the authors and right holders. Cited fragments
of works can be replaced with alternative, non-protected analogs, and as such correspond
the criteria of fair use.
All rights reserved. Any reproduction, in whole or in part, is prohibited. Agreement of the
use of works and their fragments is carried out with the authors and other right owners.
Materials from this document can be used only with resource link.
Liability for unauthorized copying and commercial use of materials is defined according
to the current legislation.