Python #8: while Loop

Python #8: while Loop

Previous topic exceeded the use of loops FOR. Depending on how many times we want to perform an action, we are closing our code in a repeating block. This way it will loop WHILEthat performs as long as the specified condition is fulfilled. Just what does it mean?

Creating a loop in Python

Remember the example from the previous part of the course:

name = input(“what is your name? “)
print(“Hello”, name)
name = input(“what is your name?”)
print(“Hello”, name)
name = input(“what is your name?”)
print(“Hello”, name)

The same thing we do with a loop for:

for user in range(0, 3):
name = input(“what is your name? “)
print(“Hello”, name)

For comparison, we can create the condition for the loop to do until counter reaches a certain value us:

counter = 0
while counter != 3:
name = input(“what is your name? “)
print(“Hello”, name)
counter += 1

Subsequent execution of the loop increases the counter by 1. Because we start from the value of the counter 0 will loop the values 0, 1, 2.

Let’s check this by adding the display of the counter value in successive iterations:

counter = 0
while counter != 3:
name = input(str(counter) + “. What’s your name? “)
print(“Hello”, name)
counter += 1

Therefore, our result looks like so:

0. What’s your name? Maria
Hi Maria
1. What’s your name? Anya
Hi Joe
2. What’s your name? South
Hi, North

WHILE loop in Python

In Python we find loops for and while.
In some programming languages, such as C++, we have, for example, also the loop in a while, in Python, the do-while loop does not occur.

WHILE starts a block of statements to be executed as long as a specified condition is true.

Condition at each performance, he re-checked and if the condition is true (returns a value True), the code in the block is executed. If the condition is not met – takes the value Falsethe code block rear loop will not execute. The interpreter will go on.

a = 5
while a > 4:
a = int(input(“Enter a number greater than 4: “))
print(“the Given number a =”, a)
print(“Your number no more than 4”)

Suppose that we want to buy a certain book, but only after the price falls below 30 rubles. Help us in Python, and in particular the loop descending order – the price of the book we will regularly lowering.

book = 59
while book > 30:
print(“Waiting for przecenę”)
book = book * 0.8
print(“Markdown 20%, the current price is: “, round(book 2), “PLN”)
print(“— — — — — — — — — — — — — — — — — —”)
print(“nide is in a bookstore!”)

Python infinite loop

If the condition will always fail cycle wykonywałaby without end.
From here guide for python while True, it’s just an endless cycle.

No need to test it, take my word for it, or look does not have the status of alloy:

while True:
print(“Hello World!”)

The same function will perform the operator is 1, because 1 is the Python True.

while 1:
print(bool(1))

Using the function bool()we will perform the conversion of a numerical value to a Boolean value:

print(bool(1))
print(bool(0))

What let’s summarize what has been done with the help of an example. Our company has expanded. On the 5th anniversary of the company we want to give you a special promo code for our first 100 customers. We know that their id in the database are the numbers from 0 to 99, from here:

Loop while includes:

  • key word “while”
  • condition
  • a block of code that will be executed in a loop (with a space)

Instructions break / continue

Still a loop (for or while) produced until it was made a condition. Sometimes we want to finish the current integration or even the whole loop before – without regular inspection of the conditional expression. Python provides us with two statements break and continueresponsible for immediate termination of the current iteration.

  • break – terminates the cycle. The interpreter moves on to the further part of the user, after the block of the loop.
  • continue – ends the current iteration of the loop. The interpreter returns to the top of the loop, the condition is checked again to determine whether the cycle will fail again if you stop and move on.

The break statement

python break interrupt cycle

For imaging, check the following code:

num = 10
while num
num += 1
if num == 15:
break
print(“Current number”, num)

print(“I have no clue”)

View sequentially values 10, 11, 12, 13, 14, if the value is 15 manual break will cause the termination of the execution of the loop and the transition to the code for the loop (appears 15). The same code works using the cycle for.

for num in range(10, 20):
if num == 15:
break
print(“Current number”, num)

print(“I have no clue”)

The continue statement

python continue the while loop

We use the same example, only now instead of breakwe use instructions continue.

num = 10
while num
num += 1
if num == 15:
continue
print(“Current number”, num)

print(“I have no clue”)

In this case you will see a value between 11 to 20 with no value 15, which is called the continue statement, i.e. it will be skipped last part of the cycle – part shows “Current room”.

for num in range(10, 20):
if num == 15:
continue
print(“Current number”, num)

print(“I have no clue”)

I hope that loop is the theme for you easy and enjoyable, therefore, time on task!

Tasks – python loop

Task 1
To write a program using while loop that for 10 consecutive positive integers and return the sum of its predecessors.
Expected result: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55

Task 2

Read the random module.

>>> import random

Create a simple guessing game. Computer random value in the range from 1 to 30. Ask the user to guess the number. The program asks the user to enter numbers until, until the player guesses.

Task 3
Extend the game in the paragraph above. The player must get the information or the amount is too big or too small.

Task 4
Write a script that calculates the value of a silnii. Solve the problem using a for loop and a while loop.
Input: “, Specify a single integer-15:” 4
Output: 4! = 24

Task 5
Using the random module, create a simple game. The computer draws a word from the available range (has a list of words). Then the letters are mixed.
Jumbled letters are shown to the player. The player has to guess what the word is. The player guesses to the result. Only guess interrupt the game.

Extension: the player can choose the keyboard “q” or “Q” to exit the game early.

This entry is part of a course Python from scratch (team work):
Python course from scratch

Ready to see us in action:

More To Explore

IWanta.tech
Logo
Enable registration in settings - general
Have any project in mind?

Contact us:

small_c_popup.png