Tia Kang Jun
Tia Kang Jun

Reputation: 9

Nested if statements in while loop

mean = "Test"

while mean.isalpha():
    mean = input("Please enter the Mean (Value must be between minus infinity (–∞) and plus infinity (+∞)): ")
    if mean == "":
        mean = 0
        break
    elif mean.isdigit():
        mean = float(mean)
        if mean <= 0:
            print("Error")

print(mean)

Could someone kindly explain to me why does my if statement in the elif loop not work? Also, why do I need a break in my first if statement.

Upvotes: -1

Views: 565

Answers (5)

Sumit Kumar
Sumit Kumar

Reputation: 760

Take following as your answer for mean

while True:
    mean = input("Please enter the Mean (Value must be between minus infinity (–∞) and plus infinity (+∞)): ")
    if mean == "":
        mean = 0
        break
    elif mean.isdigit():
        mean = float(mean)
        break
    else:
        print("error")
            
print(mean)

for variance

while True:
    variance = input("Please enter the variance (Value must be greater than 0): ")
    if variance == "":
        variance = 1
        break
    elif variance.isdigit():
        variance = float(variance)
        if variance <= 0:
            print("Error")
        else:
            break
    else:
        print("error")
            
print(variance)

Upvotes: 0

Aryerez
Aryerez

Reputation: 3495

Because the sign "-" is not a digit. So when you do .isdigit() in your elif on something with "-", it will give you False and won't reach the nested if.

To fix it you can change the elif to:

elif mean.isdigit() or ((len(mean) > 1) and (mean[0] == '-') and mean[1:].isdigit()):

To take into account negative numbers.

Or even better, use try and except on conversion to float to see if it's a valid number.

Upvotes: 0

user15801675
user15801675

Reputation:

When prompted for an input, if you simply press Enter without entering any value, then the returned value is an empty string: "".

The program is designed to catch that empty string. If you enter something, the string is not empty. So mean=="" is False. So it goes to the elif statement.

mean = 0
break

If you don't put this break, it will raise an error. This is because an integer or float doesn't have an attribute called .alpha. Once break is encountered, it exits the loop

Also, you are converting mean's datatype to floating point or integer. So it will raise an error.

mean = "Test"

while mean.isalpha():
    mean = input("Please enter the Mean (Value must be between minus infinity (–∞) and plus infinity (+∞)): ")
    if mean == "":
        mean = 0
        break
    elif mean.isdigit():
        mean = float(mean)
        if mean <= 0:
            print("Error")

print(mean)

Upvotes: 1

Sither Tsering
Sither Tsering

Reputation: 1308

You are missing one break in elif block.

elif mean.isdigit():
        mean = float(mean)
        if mean <= 0:
            print("Error")
        break

in here, you are making mean as float, and isalpha() is a string funtion. If you dont break the loop, it'll move to next iteration and while mean.isalpha(): will give error as mean has changed to float

Upvotes: 0

Raymond Hettinger
Raymond Hettinger

Reputation: 226674

The break exits the while loop.

The elif block is unnecessary and has no effect. The code block in the elif section can be unindented.

Upvotes: 1

Related Questions