Home > OS >  joining sequences together using the operator
joining sequences together using the operator

Time:08-08

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]

CodePudding user response:

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.

CodePudding user response:

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())
  • Related