Python for non-programmers, lesson 6: Loops

preview_player
Показать описание
Learn how to repeat actions in Python with loops — and master the differences between for and while loops.

Want to repeat something in your Python program? You could just write the code several times in a row... but it's better to use a "loop," in which case you say, "I want to do such-and-such a bunch of times." Python supports two kinds of loops, "for" and "while," and in this video, I explain what they are, how they're different, when to use them, and some tricks for working with them.

-----

Homework exercises for this lesson

1. Ask the user to enter a sentence. Print the number of vowels, digits, and other characters that were in the user’s text.

2. Ask the user to enter their name. Print a “name triangle,” meaning: On the first line, print just the first letter. Then on the second line, print the first two letters. Then on the third line, print the first three letters… If the name is n letter long, then there should be n lines, with the nth line printing the user’s name (as entered).

3. Ask the user, repeatedly, to enter numbers. If they enter an empty/blank string, then stop asking and print the total. If the user enters a non-number, then scold them and ask again.
Рекомендации по теме
Комментарии
Автор

🤔Loops are one of the most important tools in a programmer's toolbox. But they can be tricky!

👇 Have questions about loops? Drop them in the comments — I read them all.

📝 Want the course notebooks and practice materials?

ReuvenLerner
welcome to shbcf.ru