Forums

Python basics help, please

I'm getting more and more into Python but there's a couple of fundamental rules that I still don't get. Could some kind soul give me some guidance on this please. If I find the solution, it'll be because I'm blindly prodding around in the dark rather than knowing the rules.

This is my code:

@app.route('/heatingadjust')
def heatingadjust():
    import MySQLdb
    conn = MySQLdb.connect(host="xxxx", user = "xxxx", passwd = "xxxx", db = "xxxx")
    cursor = conn.cursor()
    cursor.execute("select score,auto,Target from Heating")
    data = cursor.fetchone()
    score = data[0]
    auto = data[1]
    targetTemp=data[2]
    if (auto == 1):
        if (score == 0):
            #Do stuff
        return str(targetTemp)
        elif (score > 4):
            #Do stuff
        return str(targetTemp)

        else:
            #Do stuff
        return str(targetTemp)
    elif (auto == 0):
        #Do stuff
    return str(targetTemp)
    return 'ok'

All I'm getting at the moment is an error:

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

I've also tried:

@app.route('/heatingadjust')
def heatingadjust():
    import requests
    import time
    import datetime
    import MySQLdb
    conn = MySQLdb.connect(host="xxxx", user = "xxxx", passwd = "xxxx", db = "xxxx")
    cursor = conn.cursor()
    cursor.execute("select score,auto,Target from Heating")
    data = cursor.fetchone()
    score = data[0]
    auto = data[1]
    targetTemp=data[2]
    if (auto == 1):
        if (score == 0):
            #Do stuff
            return str(targetTemp)
        elif (score > 4):
            #Do stuff
            return str(targetTemp)
        else:
            #Do stuff
            return str(targetTemp)
    elif (auto == 0):
        #Do stuff
        return str(targetTemp)
return str(targetTemp)

And many, many, many other permutations... obviously apart from the correct one!

Cheers Andy

Ok, getting there. Just realised I made the absolute schoolboy error of not reloading each time after making changes to my Python file. I've now gone back through the 8 million permutations I tried, reloading at each attempt, and I've got rid of the error. Still not convinced I know what I'm doing but at least it's working.

Great! Glad you're making progress.