Python WHILE Loops: How to create loops that run FOREVER

Опубликовано: 18 Февраль 2023
на канале: Python Now
80
0

Welcome to the Python Now YouTube channel! Today, we're going to talk about while loops in Python. While loops are a powerful tool in programming that allows you to repeat a block of code while a certain condition is true. This is different from for loops which execute a block of code a certain number of times. While loops allow for more flexibility and can be used in a variety of situations. In this video, we'll go over the basics of while loops and how to use them in your code. So sit back, relax, and let's get started with while loops in Python!

How to write FOR loops (+ practical tasks):
   • Loops and Iterations in Python: How to wri...  

0:00 - Introduction to While loops in Python
0:32 - Difference between For and While loops
1:02 - Simple example: how to apply While loop to check opened windows
2:22 - What is infinite loop and reasons to avoid them
2:48 - How to avoid an infinite loop
2:56 - What happens if you run an infinite loop
3:43 - Complicated example: how to apply While loop to check and close opened windows
4:43 - Practical tasks and conclusion

Task 1: Count Down to Blastoff

Use a while loop to count down from 10 to 1, printing the current number on each iteration. After the loop completes, print "Hooray!" on a new line.

Output:
10
9
8
...
2
1
Hooray!

Task 2: Reverse a String

Create a string named word with the following value: "Python"
Use a while loop to iterate over the characters in the word string and print them in reverse order. You can accomplish this by starting the loop at the end of the string and moving backwards.

Output:
n
o
h
t
y
P

Task 3: Guess the Number

Generate a random integer between 1 and 100 and store it in a variable named secret_number. Prompt the user to guess the secret number and use a while loop to continue prompting the user until they correctly guess the number. If the user's guess is too high, print "Too high" on a new line. If the user's guess is too low, print "Too low" on a new line.

Output:
Guess the secret number (between 1 and 100): 50
Too high
Guess the secret number (between 1 and 100): 25
Too low
Guess the secret number (between 1 and 100): 29
Congratulations, you guessed the secret number!

#python #while #loops


На этой странице сайта вы можете посмотреть видео онлайн Python WHILE Loops: How to create loops that run FOREVER длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Python Now 18 Февраль 2023, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 80 раз и оно понравилось 0 зрителям. Приятного просмотра!