3

I don't want to print the last comma in my loop.

countWhile = 5
while countWhile > 0:
    if countWhile == 0:
        break
    else:
        print(countWhile, end=', ')
    countWhile -= 1
print("While Loop Finished.")

Actual result: 5, 4, 3, 2, 1,

Expected result: 5, 4, 3, 2, 1

1 Answer 1

3

This is one possible solution:

countWhile = 5
myList = []
while countWhile > 0:
    if countWhile == 0:
        break
    else:
        myList.append(str(countWhile))
    countWhile -= 1
print(', '.join(myList))
print("While Loop Finished.")

I can try to look for another simpler one if you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.