Search code examples
pythonif-statementconditional-statementslogic

Why are my If-statements not working out properly?


Im trying to write a Python program where everytime I enter an input, it will return a score of 0,1,2, or 3. if my classification is less than 2, it return a score of 0 if my classification is greater than -2 and less than 2. It returns a score of 1 and so on. However, it says that the code on my elif statement is invalid syntax. How can I approach this problem?

Logic Flow

# Part 1

def fahrToCelsius():

    fahr = float(input("Enter the temperature in Fahrenheit one more time: "))

    cel = round((fahr - 32) / 1.8, 2)

    return cel

​

# Part 2

def tempClassifier(cel):

    if cel < -2:

        return 0

    elif cel >= -2 and cel <= 2:

        return 1

    elif cel > 2 and cel <= 15:

        return 2

    elif cel > 15:

        return 3

    else:

        print("The value is not a valid temperature")

​



fahr = float(input("Enter the temperature in Fahrenheit: "))

cel = fahrToCelsius()

classification = tempClassifier(cel)

​# **I NEED HELP RIGHT HERE -- see the code below**

if classification <-2:

    placeholder = "1"

elif classification >= -2 and <= 2:

    placeholder = "2"

elif classification > 2 and <= 15:

    placeholder = "3"

elif classification > 15:

    placeholder = "4"

else:

    return = "not a valid temperature"

​

print(f"A temperature of {fahr} Fahrenheit or {cel} Celsius is considered a number {placeholder} ")

This gives the error:

  File "C:\Users\playm\AppData\Local\Temp\ipykernel_9712\2457290799.py", line 27
    elif classification >= -2 and <= 2:
                                  ^
SyntaxError: invalid syntax

Solution

  • Actually, indentation is fine. The problems is this is how your line 27 should look:

    elif classification >= -2 and classification <= 2 :
    

    You need the variable compared with each condition.