Skip to main content
Quote the spec and improve formatting, spelling and grammar
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

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 and inserted 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.

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 and inserted 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' exercises,exercise; I had to think a bit more with everything being a function, bit it looks a lot neater.

Everything is in function's,functions; could iI have handle the user_inputuser input differently? Should

Should int and floats be in a separate list?

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 and inserted 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' exercises, had to think a bit more with everything being a function, looks a lot neater.

Everything is in function's, could i have handle the user_input differently? Should int and floats be in a separate list?

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 and inserted 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?

Source Link

Automate the boring stuff with python - Comma Code

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 and inserted 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' exercises, had to think a bit more with everything being a function, looks a lot neater.

Everything is in function's, could i have handle the user_input differently? Should int and floats be in a separate list?