samedi 22 février 2020

If/Elif/Else Statement in Python - Else Statement prints even though the if constraints are being met

I am in my first month of programming and I have encountered an issue in an app I'm developing. I'm sure it's fairly simple for someone experienced but I don't know how to fix it without a syntax error. Here's my code (see below for output error)

def what_to_find():
    find = (input(": "))
    if find == int(1):
        find_slope()
    elif find == int(2):
        find_elevation1()
    elif find == int(3):
        find_elevation2()
    else:
        print("Choose a number corresponding to what you want to find")


what_to_find()

So the input function works, but regardless of what number I put in (either 1, 2, or 3), the 'print' under the else command always prints. Here is what the output is for example:

What are you trying to find? 1 = Slope, 2 = Higher Elevation, 3 = Lower Elevation : 1 Choose a number corresponding to what you want to find Insert higher elevation:

So I have more code after this which creates the prompt for the higher elevation, but I just want to know how to make sure it doesn't print the else statement once it runs. Also I am using Visual Studio Code for my IDE.

From a very inexperienced coder, thanks in advance for your help!

Aucun commentaire:

Enregistrer un commentaire