while Loop. Master basic sequences, sentinel values, complex conditions, and digit processing.
Содержание:
Python Practice: The while Loop
Hands-on Exercises | From Basic Sequences to Digit Math
Part 1: Introduction to the while Loop
These exercises will help you understand how to generate sequences and control loop counters (incrementing and decrementing).
1.0 Point
Task: Write a program that reads n and prints all numbers from n down to 1 using a while loop.
📤 Example Output
Input: 5 Output: 5 4 3 2 1
1.0 Point
Task: Output the sequence of integers from 15 to 30 inclusively: 15 16 17 ... 30. Use different variables for loop counters.
📤 Example Output
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1.0 Point
Task: Output the sequence of integers: 3 5 7 9 ... 21 (from 3 to 21 with a step of 2).
📤 Example Output
3 5 7 9 11 13 15 17 19 21
1.0 Point
Task: Output the sequence of integers: 20 18 16 ... 2 (from 20 down to 2 with a step of 2).
📤 Example Output
20 18 16 14 12 10 8 6 4 2
1.5 Points
Task: Output the sequence of integers: 15 12 9 6 3 0 (from 15 down to 0 with a step of 3). Calculate and print the sum of these integers.
📤 Example Output
15 12 9 6 3 0 Sum = 45
1.5 Points
Task: Output the sequence of real numbers: 0.1 0.3 0.5 0.7 0.9 1.1 using a while loop.
📤 Example Output
0.1 0.3 0.5 0.7 0.9 1.1
Part 2: Sentinel-Controlled Loops
In these tasks, the loop runs until a specific «stop» value (sentinel) is entered. Pay close attention to initialization and updating the input inside the loop.
1.0 Point
Task: Write a program that reads integers from the user until 0 is entered, and then prints the sum of all the numbers entered (excluding the 0).
📤 Example Output
Input: 5 Input: 10 Input: 3 Input: 0 Output: 18
1.0 Point
Task: Write a program that keeps asking the user for a password until they enter the correct password "python123". Print "Access Granted" when they enter it correctly.
📤 Example Output
Input: hello Input: world Input: python123 Output: Access Granted
1.5 Points
Task: Write a program that reads numbers until a negative number is entered. Calculate and print the average of all the positive numbers entered before the negative one. If no positive numbers were entered, print 0.
📤 Example Output
Input: 4 Input: 6 Input: 2 Input: -1 Output: Average: 4.0
1.5 Points
Task: Read words from the user one by one until the word "stop" is entered. Print the total number of words entered (not counting the word «stop»).
📤 Example Output
Input: Apple Input: Banana Input: Cherry Input: stop Output: 3
Part 3: The while Loop with Complex Conditions
These tasks require more advanced logical thinking and combining multiple mathematical operations inside the loop.
1.5 Points
Task: Two integers A and B are given (A < B). Output integers between A and B (including A and B) in ascending order and output the total quantity of these numbers.
📤 Example Output
enter two numbers, please: A = 1, B = 7 1 2 3 4 5 6 7 quantity = 7
1.5 Points
Task: Read an integer n. Using a while loop, print the multiplication table for n from 1 to 10 in the format n x i = result.
📤 Example Output
Input: 3 Output: 3 x 1 = 3 3 x 2 = 6 ... 3 x 10 = 30
2.0 Points
Task: Output the sequence of real numbers: 0.0 0.5 1.0 1.5 2.0 2.5. Calculate and print the product of these numbers.
📤 Example Output
0.0 0.5 1.0 1.5 2.0 2.5 Product = 0.0
2.0 Points
Task: Read an integer N. Find the smallest power of 2 (i.e., \(2^k\)) that is strictly greater than N. Print the result.
📤 Example Output
Input: 10 Output: 16 (Explanation: 2^3 = 8, 2^4 = 16, so 16 is the smallest power of 2 greater than 10)
Part 4: Processing Digits – The Math
Use % 10 to extract the last digit and // 10 to remove it. Loop until the number becomes 0.
1.0 Point
Task: Write a program that reads a natural number n (n > 9) and prints its second digit from the right.
📤 Example Output
Input: 4567 Output: 6 Input: 123 Output: 2
1.0 Point
Task: Write a program that reads a number and counts how many of its digits are even.
📤 Example Output
Input: 1234 Output: 2 Input: 1357 Output: 0
1.5 Points
Task: Write a program that reads a number and a digit, and checks if the digit exists in the number. Print YES or NO.
📤 Example Output
Input number: 4567 Input digit: 5 Output: YES
1.5 Points
Task: Write a program that reads a number and calculates the sum of its digits. If the sum is equal to the product of its digits, print "YES", otherwise print "NO". (E.g., 123: Sum=6, Product=6 -> YES).
📤 Example Output
Input: 123 Output: YES Input: 456 Output: NO
2.0 Points
Task: Read a natural number and find its maximum digit. Print the result.
📤 Example Output
Input: 53258 Output: 8
2.0 Points
Task: Write a program that reads a number and determines if it is a palindrome (reads the same forwards and backwards). Compare the original number with its reverse.
📤 Example Output
Input: 121 Output: YES Input: 123 Output: NO
2.0 Points
Task: Read a natural number and calculate the sum of its first digit (leftmost) and its last digit (rightmost). Print the result.
📤 Example Output
Input: 5678 Output: 13 (Explanation: first digit = 5, last digit = 8, 5 + 8 = 13)