counter = 1 while counter

I need to print my statement three times using a counter loop, therefore why isn’t it producing accurate results? Could someone please help with that mistake? The application is written in Python. Does the counter loop have any errors?

  • 2

    Please add your code to your post – the question title isn’t the proper place for it. As you can see, it’s not properly formatted at all.

    – 

  • … using a counter loop” – There is no such thing as a “counter loop”. — What language are you using? This does not seem to be java. — How is this related to error-handling? — What is the exact problem? Please edit the post and clarify.

    – 

  • What do you mean by “accurate results”? What are you expecting, what are you getting?

    – 

  • Increase the counter value.

    – 

Well…, you just forgot to increment the counter 😕
In your code, you’re just reseting the counter variable instead of incrementing it.

counter = 1
while counter <= 3:
    print(f"Hello, {user_name}! This is greeting number {counter}.")
    counter += 1

print("End of program.")

That was the code with a while loop, but instead I recommend you (depending of what you want), to use a for loop like this :

for i in range(3):
    print(f"Hello, {user_name}! This is greeting number {counter}.")

And this way, you have no needed to add a counter variable and to increment it.

Leave a Comment