-1
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.

6
  • mean will only be empty when you press "Enter" when it prompts for input. And break is kind of ... optional. Here, it is just that the value was not entered. And the program was designed that an empty string is invalid. So it breaks Commented Aug 2, 2021 at 4:24
  • Hi Sujay, if i remove the break statement in my first if loops it shows the error 'int' object has no attribute 'isalpha' when i try to run the code. Commented Aug 2, 2021 at 4:29
  • Just a minute, I will post an asnwer Commented Aug 2, 2021 at 4:29
  • @TiaKangJun Basically, that's because you're treating mean as a string in lines 1 and 4, but treating it as a float in line 9. Since you're accessing a string attribute inside the loop, if mean is a float, this will fail. Commented Aug 2, 2021 at 4:31
  • @TiaKangJun, Can you please explain what do you want to achieve by this program? Commented Aug 2, 2021 at 4:36

5 Answers 5

1

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.

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

Comments

1

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)

3 Comments

Thanks for the explanation sujay. However, i thought that if the variable is an int or float, then mean.isalpha = False. Thus shouldn't it break out of the while loop
I was thinking that the conversion should be only temporary, i.e. changing if mean <= 0 to if float(mean) <= 0.
.isalpha only works on strings. A float or an int can't contain letters so if you know the variable is a float or an int such a check is pointless.
0

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

3 Comments

The code runs just fine if i do it like this though. 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) print(mean)
@TiaKangJun try with 34 as input, or any other number
thanks so much for letting me know my mistakes. i just tried it and it indeed shows an error if I enter any other number!
0

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.

3 Comments

Hi Aryerez, if that is the case how do I fix this?
@TiaKangJun Added fixes
thank so much for the help. that helped me solved the problem I had! if you don't mind, would mind showing me how the try and except statement will help me here?
0

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)

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.