For Loop Prints an Extra Comma

For loop prints an extra comma

Use

cout << "Set B : {";

for (i = 0; i < b; ++i) {
if (i > 0) cout << ",";

cout << setB[i];
}

cout << " }" << endl;

I changed your algorithm :

Before it meant : "Put the number and then put a comma"

Now it means : "If there is a number behind me put a comma, then put the number"

Before, you always printed a comma when you printed a number so you had an extra comma.

Printing extra comma in while loop - Python

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.

How to remove extra comma after the last number in loop

For example

for(int i=0; i < something.length; i++){
System.out.print(i + i==something.length-1 ? "":" ,");
}

How to avoid last comma in python loop

Pass the results as separate arguments and specify the separator:

 print(*result, sep=',')

How do I add a comma after every word in a for loop

You can produce a list with multiple elements by using the * operator. Then you can join() them together:

for i in range(10):
print(', '.join([user_input] * (i + 1)))


Related Topics



Leave a reply



Submit