vendredi 30 juillet 2021

Can I add IF clause to TRY & EXCEPT clause

Please pardon me if I make any formatting issues here!

I am creating 2 variables, namely the mean and variance. Still working on the "variance" variable so please ignore that part.

Here's the background: The mean and variance are int inputs. The mean can be of any value, on the other hand, the variance will need to be of value greater than 1. And whenever, the input prompts, and the users hit enter straightaway (without any inputs), the mean and variance will be 0 and 1 respectively.

I have attempted the question and want to integrate both the TRY & EXCEPT clause with the IF clause because to check of ValueError and to automatically stop the program if the users hit enter without any values...

I am not sure if this is doable, can anyone advise, please... Many thanks!

Here's my code:

mean = 0
variance = 1

valid_input = False
while valid_input == False:
    try:
        mean = int(input("Please enter the mean value: "))
        if mean == "":
            valid_input = True
            break
    except ValueError:
        print("Please enter a numeric value!")
    else:
        valid_input = True
print (mean, variance)

Aucun commentaire:

Enregistrer un commentaire