joining sequences together using the + operator

Question:

enter image description here

Does anyone know how to write this out in code using the + operator?
I’ve been trying for a while but I don’t know how to get the output to be in the format:
Data: [1, 2, 3, 4]

Asked By: Georgie Walsh

||

Answers:

This is my first time answering so I apologize in advance if my answer is unnecessarily complicated. First question I have is, do you have to use the ‘+’ operator? If not, you could just append the numbers into a list (we can call the list ‘ls’) and then if you do print(ls) python should automatically print out the list like you want.

If you do have to use the ‘+’ operator, you could have a string with an opening bracket, so just ‘[‘ and then every number they type in, you could add each number to the string using a loop.

Answered By: nickdavila

Try this, not sure why do need + operator here, unless you want to write result += [int(value)]:

def main():
    result = []
    while True:
        value = input("Enter an integer number (blank to exit): ")
        if value == "":
            return f'Data: {result}'
        result.append(int(value))


print(main())
Answered By: funnydman
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.