dimanche 3 juillet 2016

PYTHON If statement within a while loop

My code crashes when something other than "Y" or "N" is inputted, and because of the if statement and while loops, I don't know how to fix it. I understand why it doesn't work (there's no else on the if statement), but I don't know how to effectively fix it.

ask = "g"
while ask != "Y" and ask != "N":
    ask = input("Do you want a random disease factor? Enter Y if you do, or N if you want to select it yourself. Remember this will be between 0.2 and 0.5")
    if ask == "Y":
        disease = random.uniform(0.2, 0.5)
    elif ask == "N":
        disease = 12
        while disease < 0.2 or disease > 0.5 :
            try:
                disease = float(input("What is your selected disease factor? Remember this should be between 0.2 and 0.5 : "))
            except ValueError as ex:
                print(ex)

Aucun commentaire:

Enregistrer un commentaire