0
counter = 1 
while counter:
    print(counter) 
    counter=counter + 1 
    if counter==True:
        print("NOPE, False") 
    if counter == 0:
        print("NOPE, 0") 
    if counter==False:
        print("YES, True") 
    elif counter==(0 or False):
        print("YES")

What am I doing wrong? For what reason do I have repetition 111111 at the output? I want it to alternately print the texts below depending on whether the number is zero or some other number?

Update: Thanks for the help, I tried to create a program that will change values (increase number) and print a message with a while-loop. After your help and clarification, it is clear that without more conditions and redirects I will not be able to get different outputs / text, so the task itself is not well set at the beginning. Now I look stupid to myself when I look at my idea and this attempt...

1
  • why are you checking INTs against BOOLs? Commented Jun 24, 2020 at 20:40

3 Answers 3

2

Positive integers are evaluated as True and counter is always positive. Thus the while-loop will run infinetly.

I don't know what the goal of your code is, but usually when you work with counters a while-loop is of the form:

while counter <= max_iter:

This will execute the while-loop until the counter reaches the value of max_iter.

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

Comments

1

The answer is because counter is a variable of type int, you need to convert it to a boolean type, like so:

counter = 1 
while counter:
    print(counter) 
    counter=counter + 1 
    if bool(counter) == True:
        print("NOPE, False") 
    if counter == 0:
        print("NOPE, 0") 
    if bool(counter) == False:
        print("YES, True") 
    elif bool(counter) == False or counter == 0:
        print("YES")

Comments

0

in this case example you have written the value of counter will always be "Truthy". If you write while counter: the conditional will always be true unless you set the value of counter to be either specifically either 0 or False.

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.