2017-07-14 55 views
-3

我的狀況有問題。我想變tabPoint爲10和100之間條件「和」,「或」

這裏是我的代碼:

def demand(nb): 
    tabName = []; 
    tabPoint = []; 

    for i in range(nb): 
    tabName.append(raw_input("Name of the jumper " + str(i+1) + " : ")) 
    tabPoint.append(input("1st jump " + tabName [i] + " The number must be between 10 and 100: ")); 

    if int (tabPoint[i] < 5) and int (tabPoint[i] > 100): 
     tabPoint.append(input("The number must be between 10 and 100 ")); 

    return tabName, tabPoint; 

name, point = demand(3) 
print(name, point) 
+2

1)你的圓括號在錯誤的地方。 2)小於5且*大於100 *?這怎麼可能? – asongtoruin

+0

你也不需要';'在行結尾 – depperm

+3

你希望它小於5 *和*大於100?這不可能。你的意思是*還是*? – Carcigenicate

回答

0

你遺失了你的括號內。你想要的int是tabPoint[i],而不是tabPoint[i] < 5

所以正確的方式是

if int(tabPoint[i]) > 5 and int(tabPoint[i]) < 100: 
    tabPoint.append(input("The number must be between 10 and 100 ")) 

您也可以使用實現相同的短版:

if 5 < int(tabPoint[i]) < 100: 
    tabPoint.append(input("The number must be between 10 and 100 ")) 
+0

邏輯沒有任何意義,如果<5 and > 100在10到100之間怎麼樣? – depperm

+0

@depperm右鍵,更正。 –

+0

5-100!= 10-100,即使OP提到它 – depperm

0

試試這個:

def demand(nb): 
    tabName = [] 
    tabPoint = [] 

    for i in range(nb): 
     tabName.append(input("Name of the jumper "+str(i+1)+": ")) 
     tabPoint.append(0) 

     # Until a valid entry is made, this prompt will occur 
     while tabPoint[i] < 10 or tabPoint[i] > 100: 
      tabPoint[i] = (int(
       input("1st jump "+tabName[i]+" The number must be between 10 " 
         "and 100: "))) 


    return dict(zip(tabName, tabPoint)) # Returning a dictionary mapping name to point 

說你想打印在此之後的每個名稱和點,你可以實現這樣的:

info = demand(3) 

for name, point in info.items(): 
    print(f"Name: {name} Point: {point}")