Tuesday, November 15, 2022

Lists - Python Programming for Beginners Practice Exercise 8

Exercise 8:

Create a Python program that captures and displays a person's to-do list. Continually prompt the user for another item until they enter a blank item. After all the items are entered, display the to­do list back to the user.

Sample Output:


Enter a task for your to-do list. Press <enter> when done: Buy cat food.
Task added.
Enter a task for your to-do list. Press <enter> when done: Mow the lawn.
Task added.
Enter a task for your to-do list. Press <enter> when done: Take over the world.
Task added.
Enter a task for your to-do list. Press <enter> when done:

Your To-Do List:
-------------------
Buy cat food.
Mow the lawn.
Take over the world.

Solution:

#!/usr/bin/env python3

# Create a list to hold the to-do tasks.
to_do_list = []
finished = False
while not finished:
    task = input('Enter a task for your to-do list.  Press  when done: ')
    if len(task) == 0:
        finished = True
    else:
        to_do_list.append(task)
        print('Task added.')

# Display the to-do list.
print()
print('Your To-Do List:')
print('-' * 16)
for task in to_do_list:
    print(task)

No comments:

Post a Comment