2017-04-25 62 views
-5

我想爲x的所有不同值評估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" 
+3

嘗試移動'X =(P * L * Y)/ I'到你的'for'循環中。使用'i'而不是'L'。 –

+1

'L()'也不對。 –

+0

'L'是一個列表,而不是一個函數。 –

回答

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

你沒有帶指定其將要採取的L價值,並沒有對x

1

我想你會想什麼指定的y值的公式中是一樣的東西:

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" 
+0

這不會執行:**我**通過** L **元素的步驟;這不是一個合適的指數。 – Prune

+0

你說得對。我編輯我的答案使用一個while循環。另外OP需要爲y聲明一個值。 – Wiredo

+0

請在發佈之前運行您的代碼。我在前面瞭解了很多點。 – Prune

1

我猜測,但這種修復你的語法和語義錯誤:

  • 你沒有定義Ÿ
  • 您對列表訪問大號在幾個地方
  • 你的計算是外循環是不正確的,所以X從未 改變
  • 您在打印同樣的事情您如果

代碼的兩個分支:

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