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:

  1. #!/usr/bin/env python3
  2.  
  3. # Create a list to hold the to-do tasks.
  4. to_do_list = []
  5. finished = False
  6. while not finished:
  7. task = input('Enter a task for your to-do list. Press when done: ')
  8. if len(task) == 0:
  9. finished = True
  10. else:
  11. to_do_list.append(task)
  12. print('Task added.')
  13. # Display the to-do list.
  14. print()
  15. print('Your To-Do List:')
  16. print('-' * 16)
  17. for task in to_do_list:
  18. print(task)

No comments:

Post a Comment