Tagged: loops
Everything tagged "loops", newest first.
Python Coding Exercise #62: Substrings, Parts 1 and 2
Two beginner Python exercises on slicing in a loop — print all the substrings of a word that start with its first character, then all the substrings that end with its last character, using for loops and while loops.
Python Coding Exercise #59: Underlining
A beginner Python exercise on loops, len() and string multiplication — keep asking for strings, print each one underlined with dashes of the same length, and stop when the user presses Enter on an empty line.
Python Coding Exercise #58: A Line and a Rectangle of Hashes
Two beginner Python exercises on repeating output — print a line of hash characters of a chosen width, then repeat that line to draw a rectangle, with a for loop and a while loop compared.
Python Coding Exercise #56: End to Beginning
A beginner Python exercise on string indexing and while loops — print a string in reverse, one character per line, by counting an index down from -1 using negative indexes.
Python Coding Exercise #53: The Sum of Consecutive Numbers
Two beginner Python exercises on a while loop that adds 1, 2, 3 and so on until the total reaches a limit, then builds the calculation as text to print it too.
Python Coding Exercise #52: Powers of Two and Powers of Base n
Two beginner Python exercises on a while loop that multiplies — print the powers of two up to a limit, then change the program so the user chooses the base.
Python Coding Exercise #51: Even Numbers, Countdown and Counting Up
Three beginner Python exercises on while loops with a condition instead of while True — print the even numbers up to 30, fix a broken countdown, and print every number below a limit.
Python Coding Exercise #50: Working with Numbers
A beginner Python exercise on collecting statistics in a loop — read numbers until the user types zero, then print the count, the sum, the mean, and how many numbers were positive and negative.
Python Coding Exercise #49: Story
A beginner Python exercise on building a string inside a loop — collect words from the user until they type "end" or repeat the previous word, and learn how to remember the previous value.
Python Coding Exercise #48: The Next Leap Year
A beginner Python exercise on searching with a while loop — start from the year after the one entered, test each year against the leap year rule, and stop at the first one that fits.