Vinay Kurmi
Vinay Kurmi

Reputation: 77

python loops does not break even after successful while statement

My while loop does not break even after successful while recall. I need to check if the number exists and if it doesnt my while loop should break and successfull print out the number apart from 1 / 3 or 6

def is_1(inp):
if inp == 1:
    print("1 it is")
    return False

def is_3(inp):
    if inp == 3:
        print("3 it is")
        return False
    
def is_6(inp):
    if inp == 6:
        print("6 it is")
        return False
    
# ------------------------
me = False
while me != True:
    try:
        inpp = int(input("Please enter a number : "))
        if any([is_1(inpp),is_3(inpp),is_6(inpp)]) == False:
            me = False
        else:
            me = True
            print(inpp)
    except IndexError:
        print('Inappropriate domain and username')

enter image description here

Upvotes: 0

Views: 275

Answers (3)

EEAH
EEAH

Reputation: 747

The problem is_1, is_3, and is_6 will evaluate to none if inp != 1, and because of the code also to false if inp ==1

def is_1(inp):
    if inp == 1:
        print("1 it is")
        return False

2-

any([item1, item2, ....]) returns true if any (or at least one) of the items is true. Otherwise It returns false

And because is_1 and is_3 and is_6 always return false or none, any([is_1(inpp),is_3(inpp),is_6(inpp)]) will always be false and me will always be false.

To fix the issue, is_1 and is_3 and is_6 needs to return a true value:

def is_1(inp):
    if inp == 1:
        print("1 it is")
        return True

def is_3(inp):1
    if inp == 3:
        print("3 it is")
        return True
    
def is_6(inp):
    if inp == 6:
        print("6 it is")
        return True
    
# ------------------------
me = False
while me != True:
    try:
        inpp = int(input("Please enter a number : "))
        if any([is_1(inpp),is_3(inpp),is_6(inpp)]) == False:
            me = False
        else:
            me = True
            print(inpp)
    except IndexError:
        print('Inappropriate domain and username') 

Upvotes: 0

Henrik
Henrik

Reputation: 928

You just add a break and rewrite you if statements. That would break your loop. (Also according to what @p3j4p5 wrote)

Here is the code:

def is_1(inp):
    if inp == 1:
        print("1 it is")
        return False

def is_3(inp):
    if inp == 3:
        print("3 it is")
        return False
    
def is_6(inp):
    if inp == 6:
        print("6 it is")
        return False
    
# ------------------------
me = False
while me != True:
    try:
        inpp = int(input("Please enter a number : "))
        if inpp not in (1, 3, 6):
            me = True
            print(inpp)
            break
        elif any([is_1(inpp),is_3(inpp),is_6(inpp)]) == False:
            me = False
    except IndexError:
        print('Inappropriate domain and username')

Now if the input is not in 1 or 3 or 6 it will break. Is this the solution?

Upvotes: 0

Paul P
Paul P

Reputation: 3907

Each of the functions is_1(), is_3(), is_6() returns None when the respective condition is not met.

This means, if you enter any number other than 1, 3, or 6, this will lead to an array containing only Nones in the line

if any([is_1(inpp),is_3(inpp),is_6(inpp)]) == False:

i.e.

if any([None,None,None]) == False:

This, in turn, will evaluates to False.

In other words, the line me = True is never reached.

In order to fix this, you need to make the three methods above return something that evaluates to True if the condition isn't met (i.e. when you are passing in anything else than 1,3, or 6).

Upvotes: 4

Related Questions