Comma Code
Say you have a list value like this:
spam = ['apples', 'bananas', 'tofu', 'cats']Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with
andinserted before the last item. For example, passing the previous spam list to the function would return'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it.
# Function for getting user input.
def get_input():
user_input = input('Enter some random stuff: ')
return user_input
# Function for appending the input to the list
def logic_code(input, big_list):
big_list.append(input)
return big_list
# Function for printing the output
def print_output(big_list):
while True:
input = get_input()
if input == 'Done': # If user input == 'Done', break loop.
break
logic_output = logic_code(input, big_list) # Output of logic_code
output = print(logic_output)
return output
def main() -> None:
big_list = []
print('Type Done to stop.')
output = print_output(big_list)
if __name__ == '__main__': # Program starts here, then main() get's called.
main()
This was a 'simple' exercise; I had to think a bit more with everything being a function, bit it looks a lot neater.
Everything is in functions; could I have handle the user input differently?
Should int and floats be in a separate list?
and\$\endgroup\$