joining sequences together using the + operator
Question:
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.
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())
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.
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())