using 3 variables in the beginning of an IF statement in python

时间:2016-04-04 18:40:46

标签: python if-statement

I have a web page that displays all of the accounts. Underneath it, I have the option where they can have the table highlight certain rows depending on the Balance column. My question is if I can create an if statement where the first line contains only variables (including the greater than, equal to, etc option). My original set up is that they would select from a drop down menu if they want >, >=, =, <=, or < and then the second part is they would put in the amount. I am guessing the if statement doesn't like that I have an option for them to select the operator.

Here is the code for the form part of my web page:

#form
print("<form action='http://localhost:8000/cgi-bin/Bank_ViewAll.py'>")
print("<center>Highlight rows based on balance amounts!</center>")
print("<BR><BR>")
print("<center>Balance:&nbsp;")
print("<select name='option' style=background:#eef9a0> &nbsp; $")
print("<option value='""' style=background:#eef9a0>Choose an  Option</option>")
print("<option value='>' style=background:purple>Greater Than (>)</option>")
print("<option value='>=' style=background:blue>Greater Than or Equal To (>=)</option>")
print("<option value='=' style=background:yellow>Equal To (=)</option>")
print("<option value='<' style=background:orange>Less Than (<)</option>")
print("<option value='<=' style=background:red>Less Than or Equal To (<=)</option>")
print("</select>")
print("<input type='text' size='15' name='bal' style=background-color:#eef9a0>")
print("<br><BR>")
print("<center>")
print("<input type='submit' class='buttonStyle' value='Submit'>&nbsp;&nbsp;")
print("<input type='reset' class='buttonStyle' value='Clear fields'>")
print("</center>")
print("</form>")
#end form

And here is the code for my function:

import cgi, cgitb, sqlite3
cgitb.enable()
sqlite_file="C:\\Users\\U49228\\Desktop\\Class exercises\\Banking.sqlite"
connect=sqlite3.connect(sqlite_file)
cursor=connect.cursor()

form=cgi.FieldStorage()
bal=int(form.getvalue('bal'))
opt=form.getvalue('option')

print("Content-type:text/html\r\n\r")
print()

print("<html>")
#Header
print("<head>")
print("<title>View Accounts Functions</title>")
print("</head>")
#body
print("<body bgcolor=#d4f4b0>")
#table
print("<center>")
print("<table border=2>")
print("<th>Acct_No</th>")
print("<th>Name</th>")
print("<th>Balance</th>")
def high_bal():
    cursor.execute("Select * from Banking_Accounts")
    all_rows=cursor.fetchall()
    for row in all_rows:
        accno=row[0]
        cname=row[1]
        amt=int(row[2])
        if amt opt bal:
            print("<tr bgcolor='cornsilk'>")
        else:
            print("<tr>")
        print("<td>"+str(accno)+"</td>")
        print("<td>"+cname+"</td>")
        print("<td>"+str(amt)+"</td>")
        print("</tr>")
    print("</table>")
    print("</center>")
    #end table
    print("<br><BR>")
    print("<center>")
    print("<a href='http://localhost:8000/cgi-bin/Bank_Menu.py' style=color:black>Return to Main Menu</a>")
    print("</center>")
    print("</body>")
    print("</html>")

I planned on importing my function page. I have tried adding the variables inside the function (def high_bal(opt,bal)) as well as trying onClick on the submit button and onSubmit on the form. I've also tried just making the form redirect to the function page and removing the function name so it is redirecting to a page that will go ahead and do the function without it being called.

Screen shot of my web page design:

enter image description here Thank you for your time and help!

2 个答案:

答案 0 :(得分:1)

if amt opt bal:

is not a valid python syntax, you can either create a functions for each of the operators like for example

operators = {
  '=': lambda x, y : x == y,
  '<': lambda x, y : x < y,
  '>': lambda x, y : x > y,
}

if operators[opt](amt, bal):

the other way around (dangerous) is to evaluate the expression:

if eval("amt %s bal" % opt):

答案 1 :(得分:0)

You can use the operator module:

from operator import gt, ge, eq, le, lt

ops = {'>': gt, '>=':  ge, '=': eq, '<=': le, '<': lt}

Now you can do:

if ops[opt](amt, bal):

Example:

>>> opt = '>'
>>> ops[opt](3, 1)
True
>>> ops[opt](3, 10)
False