My code looks like this at the moment:
limit = int(input("Limit:"))
number = 1
sum = 1
while sum < limit:
number = number + 1
sum = sum + number
print(f"The consecutive sum:{sum}")
Add the numbers you're using in a separate list. Then use str.join()
to join these numbers with a ' + '
.
limit = int(input("Limit:"))
number = 1
total = number
numbers = [str(number)]
while total < limit:
number = number + 1
total = total + number
numbers.append(str(number)) # Need to convert to string here because str.join() wants a list of strings
print(f"The consecutive sum: {' + '.join(numbers)} = {total}")
Which prints the required output:
The consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 = 21