Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions projects/009-formatting-a-list/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def formatting_list (item_list):
items_number = len(item_list)

if items_number <= 1:
string_result = ''.join(item_list)

elif items_number == 2:
string_result = ' and '.join(item_list)

else:
first_slice = ', '.join(item_list[:items_number-1])
second_slice = ''.join(item_list[items_number-1:])
string_result = first_slice + ' and ' + second_slice

return string_result

list_from_user = []

user_input = input('insert an item (leave blank to stop): ')
while user_input != '':
list_from_user.append(user_input)
user_input = input('insert an item (leave blank to stop): ')

print(formatting_list(list_from_user))