Python Coding Exercise
This Python countdown challenge is a great Python coding exercise for beginners to practice loops along with functions. Your task is to write a Python 3 function that counts down from any given number to zero. The program should prompt the user to enter the starting number for the countdown and meet the following requirements:
- Ask the user to enter the starting number for the countdown.
- The countdown should include the starting number and reach zero.
- The countdown should be displayed in descending order.
- The countdown must pause for 1 second between each number.
- The function should be able to handle any positive integer as the starting number.
Guide
To complete this Python assignment follow these steps:
- Import the time module to use the
time.sleep(x)function. - Define a function
countdownthat takes the starting number as input. - Inside the function, use a while loop to iterate from the starting number down to 0.
- Inside the while loop, print the current number and then use
time.sleep(1)to pause for 1 second. - Decrement the current number in each iteration of the loop until it reaches 0.
- Prompt the user to input the starting number for the countdown.
- Call the
countdownfunction with the user-input starting number.
Expected Output
Enter the starting number for the countdown: 10
10
9
8
7
6
5
4
3
2
1
0
For more Python exercises and challenges , visit our store.
Source code
import time
def countdown(start):
while start >= 0:
print(start)
time.sleep(1)
start -= 1
start_number = int(input("Enter the starting number for the countdown: "))
countdown(start_number)