2016-04-04 5 views
-2

すべてのアカウントを表示するWebページがあります。その下には、バランス欄に応じてテーブルに特定の行をハイライト表示させるオプションがあります。私の質問は、ifステートメントを作成することができます。最初の行には、より大きい、等しい、等のオプションを含む変数のみが含まれています。 オリジナルの設定では、>>==<=、または<が必要な場合はドロップダウンメニューから選択し、次に2番目の部分は金額に入れます。私はifステートメントは、私は彼らがオペレータを選択するためのオプションが好きではないと思います。ここでPythonのIF文の先頭に3つの変数を使用

は私のWebページの一部を形成するためのコードです:

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>") 

私は機能ページをインポートするには、計画:

#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 

そして、ここでは私の関数のコードです。私は関数内の変数(def high_bal(opt,bal))を追加しようとしただけでなく、onClickを送信ボタンに、onSubmitをフォーム上に試してみました。また、フォームを関数ページにリダイレクトし、関数名を削除して、先に進むページにリダイレクトし、呼び出さずに関数を実行するようにしました。私のWebページデザインの

スクリーンショット:

enter image description here あなたの時間と助けてくれてありがとう!

答えて

0

あなたはoperator moduleを使用することができます。

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

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

今、あなたは行うことができます。

if ops[opt](amt, bal): 

例:

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

スペクタキュラーを!これは私のために働いた!どうもありがとうございます! – Tigger

1
if amt opt bal: 

が有効なPythonの構文ではありません、あなたは例えば

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

if operators[opt](amt, bal): 

のような演算子のそれぞれの関数を作成することができますいずれかの周りの他の方法は、(危険な)の発現を評価することである。

if eval("amt %s bal" % opt): 
関連する問題