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.
meanwill only be empty when you press "Enter" when it prompts for input. Andbreakis 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 breaksmeanas 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, ifmeanis a float, this will fail.