2015-05-12 35 views
-1
def searchQueryForm(alist): 
noforms = input("how many forms do you want to search for") 
for i in range(noforms): 
    searchQuery = [ ] 
    nofound = 0 ## no found set at 0 
    formname = input("pls enter a formname >> ") ## asks user for formname 
    formname = formname.lower() ## converts to lower case 
    for row in alist: 
     if row[1].lower() == formname: ## formname appears in row2 
      searchQuery.append(row) ## appends results 
      nofound = nofound + 1 ## increments variable 
      if nofound == 0: 
       print("there were no matches") 
       return searchQuery 

我想用一個變量來設置循環發生的次數。我試圖通過輸入一個變量來設置它。但我不斷收到此錯誤使用一個循環,同時使用一個變量來設置它循環的次數

TypeError: 'str' object cannot be interpreted as an integer 
+2

這是發佈錯誤的完整回溯幫助的地方。你的錯誤不涉及'nofound',但它實際上涉及'noforms'。 – TerryA

+2

您不會通過刪除所有代碼來改善您的問題。根據要求,通過發佈回溯來改進它。 –

+0

如果答案沒問題,請接受它@Rasnaam Tiwana –

回答

0

python3使用:noforms = int(input("tldr"))所以您將獲得價值爲int,而不是string

0

演員輸入到intinput在python3中返回一個字符串。

int(input("how many forms do you want to search for")) 

你的錯誤是從傳遞到STR range

In [1]: range("100") 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-1-5ca20de8d687> in <module>() 
----> 1 range("100") 

TypeError: 'str' object cannot be interpreted as an i 
1

角色的用戶輸入。然後你會得到一個int而不是string

noforms = int(input("how many forms do you want to search for"))

-1

我不知道代碼你怎麼看,但也可能是這樣的:

var1 = "33" 
for i in range(var1): 
    print(i) 

如果是這樣的話,從數字中刪除引號「」,所以它看起來像這樣。

var1 = 33 #Quotation marks removed 
for i in range(var1): 
    print(i) 
相關問題