Search code examples
pythonpymysql

pymysql-How to add cases on my code


So I am trying to get some cases to work on my code but I get all the time the same error TypeError("'>=' not supported between instances of 'str' and 'int'",). Can somebody help me? The code is this:

try:
    float(rank1)
except ValueError:
    return [("status",),("error",),]
try:
    float(rank2)
except ValueError:
    return [("status",),("error",),]
if (rank1>=0 and rank1<=10 and rank2>=0 and rank2<=10):
    sql_query='''update movie set rank=(%s + %s)/2 
    where movie.title=%s'''
    cur.execute(sql_query,(rank1,rank2,movieTitle))
    con.commit()
    return [("ok",)]
else :
    return [("status",),("error",),]

Solution

  • I think what you probably want is:

    try: 
        rank1 = float(rank1) 
    except ValueError: 
        return [("status",),("error",),] 
    try: 
        rank2 = float(rank2) 
    except ValueError: 
        return [("status",),("error",),] 
    
    if (rank1>=0 and rank1<=10 and rank2>=0 and rank2<=10):
        sql_query='''update movie set rank=(%s + %s)/2 where movie.title=%s'''
        cur.execute(sql_query,(rank1,rank2,movieTitle)) 
        con.commit() 
        return [("ok",)] 
    else: 
        return [("status",),("error",),]