For和If功能

时间:2017-04-25 18:34:01

标签: python

我想要为L的所有不同值评估x,然后打印结果。 我在这里做错了什么?

P=3
I=2
L = [2,4,6,8,10]

x = (P * L * y)/ I

for i in L():
    if x <= 305:
        print "this" + L() + "will not work"
    else:
        print "this" + L() + "will not work"

4 个答案:

答案 0 :(得分:1)

我认为你想要的是:

while i < len(L):
    x = (P * L[i] * y)/ I
    if x <= 305:
        print "this" + L[i] + "will work"
    else:
        print "this" + L[i] + "will not work"

答案 1 :(得分:1)

我猜,但这会修复你的语法和语义错误:

  • 您没有定义 y
  • 您在多个地方对 L 列表的访问权限不正确
  • 你的计算在循环之外,所以 x 永远不会 改变了
  • 您在 if
  • 的两个分支中打印了相同的内容

代码:

P=3
I=2
L = [2,4,6,8,10]
y = 1

for i in L:
    x = (P * i * y)/ I
    if x <= 305:
        print "this", i, "is small enough"
    else:
        print "this", i, "will not work"

输出:

this 2 is small enough
this 4 is small enough
this 6 is small enough
this 8 is small enough
this 10 is small enough

答案 2 :(得分:1)

根据您的要求,您应该遍历L中的所有值。

试试这段代码

P=3
I=2
L = [2,4,6,8,10]
x=[]
y=?

for i in L:
      x.append((P * i * y)/I)

for idx,i in enumerate(x):
    if x <= 305:
        print "this" + L[idx] + "will not work"
    else:
        print "this" + L[idx] + "will not work"

答案 3 :(得分:0)

您没有在L的等式中指定y的值x且未指定<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title></title> <style type="text/css"> @media only screen and (min-width: 100px) and (max-width: 625px) { td, span { font-size: 11px!important; } #tab td { width: 40%!important; word-break: break-all; display: table-cell; } } </style> </head> <body style="margin: 0; padding: 0; min-width: 100%!important; font:15px/21px 'Arial';"> <!-- background table --> <table border="0" cellpadding="0" cellspacing="0" width="100%" id="bodyTable"> <tr> <td align="center" valign="top"> <!-- table width setter --> <table border="0" cellpadding="0" cellspacing="0" style="width: 100%; max-width: 600px; padding-right: 10px; padding-left: 10px;" id="emailContainer"> <tr> <td align="center" valign="top"> <table border="0" cellpadding="0" cellspacing="0" width="100%" id="emailHeader"> <tr style="width:100%; height:30px;"> <td> &nbsp; </td> </tr> </table> </td> </tr> <tr> <td align="center" valign="top"> <table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-top:5px solid #fac22c;"> <tr style="height:30px;"> <td> &nbsp; </td> </tr> <!-- begin content --> <tr> <td align="center"> <table cellspacing="0" cellpadding="9" border="1" style="width:auto; height:auto; background-color:#f6f6f7; border-color:#f2f2f2;" id="tab"> <tbody> <tr style="width:100%; height:auto;"> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test2</td> <td style="width:10px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test3</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test4</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test5</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test6</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test7</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test8</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test9</td> <td style="width:50px; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test10</td> <td style="width:undefinedpx; height:auto; color:#232321; font:bold 12px/21px 'Arial';">Test11</td> </tr> <tr style="width:100%; height:auto;"> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">123123</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Some Person</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Unassociated Person</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Unassociated Person</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Something in the Works</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">The Gates Account</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">This isn't a main bucket</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">John Smith</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">576-123-5566</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">4/12/1992</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">3/4/1020</td> </tr> <tr style="width:100%; height:auto;"> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">123123</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Some Person</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Unassociated Person</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Unassociated Person</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">Something in the Works</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">The Gates Account</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">This isn't a main bucket</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">John Smith</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">576-123-5566</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">4/12/1992</td> <td style="width:50px; height:auto; color:#232321; font: 12px/21px 'Arial';">3/4/1020</td> </tr> </tbody> </table> </td> </tr> <!-- begin content --> </table> </td> </tr> </table> <!-- table width setter --> </td> </tr> </table> <!-- end background table --> </body> </html> 的值