lundi 1 novembre 2021

Syntax error help - Try statement not "seeing" my except clause and if statement expecting else and not finding (though it's there) [closed]

I'm new to Python but trying to put together a relatively simple data processing programme. I am having issues with a specific function to do with getting user input (just through console) for maximum values. With the input I'm attempting to use Try and Except to avoid crashing is the value entered is not convertible to a float for any reason.

Unsure why I'm seeing errors. Apparently my Try statement in the code is looking for either an Except or Finally, and I do have an Except present it just doesn't seem to recognise it.

Secondly, could be somehow the same issue, within the Try statement I have an If statement that is apparently expecting an Else statement to be following (which it is) but again can't seem to find it.

Am I missing something simple here? Is it linked to indentations or something?

Code for the function is below:

def userMax(limiter):
    
    if limiter == "STANDARD DEVIATION" or "STANDARD ERROR":
        max = 1.0
        # Max starts as 1 as it is a high value which shouldn't be seen in any data, and so is reasonable to limit the SD and SE at even if user does not define a max.

    elif limiter == ("ABSOLUTE TILT (for X and Y)"):
        max = 1000.0
        # Max starts as 1000 as it is a high value which shouldn't be seen in any data, and so is reasonable to limit the tilt values at even if user does not define a max.

    else:
        max = 9999.0
    
    limit = input("Is there a maximum %s you would like a reading to have to be included in calculating the median gravity reading? (y/n):" % limiter)

    while limit != ("y" or "n"):
        limit = input("Please only answer with ""y"" for yes, or ""n"" for no:")
        # Ensures only y or n (yes or no) can be entered here.
    
    if limit == "y":
        while True:
        # While loop is to ensure any invalid input will not get passed into the rest of the program.

            try:

                # Taking user input for values.
                maxInput = float(input("What would you like the %s to be limited to for readings to be considered? (%d is the absolute maximum):" % (limiter, max))

                if maxInput <= 0:
                    print("Please enter a value greater than zero.")

                else:

                    if maxInput > max:
                        maxInput = max

                break
                # Break exits while loop once a valid maximum has been inputted by the user.
                
            except ValueError:
                print("Please enter a numeric value.")
                # Except clause allows for incorrect values (i.e. letters or special characters) to be input and the user to re-input data instead of crashing the programme.

        max = maxInput 
        # Sets value to be returned to value input by the user, if they've chosen to set a maximum.

    return(max)

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire