使用循环,同时使用变量来设置它循环的次数

时间:2020-12-01 23:54:25
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

i am trying to use a variable to set how many times a loop occurs. i have tried to set it by entering a variable. but i keep getting this error

我试图使用一个变量来设置循环发生的次数。我试图通过输入变量来设置它。但我一直收到这个错误

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

4 个解决方案

#1


Cast the user input. Then you will get an int instead of a string:

投射用户输入。然后你会得到一个int而不是一个字符串:

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

noforms = int(输入(“你要搜索多少种形式”))

#2


In python3 use: noforms = int(input("tldr")) so You will get value as a int, not a string

在python3中使用:noforms = int(输入(“tldr”))所以你将获得值作为int,而不是字符串

#3


Cast the input to an int. input returns a string in python3.

将输入转换为int。 input在python3中返回一个字符串。

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

Your error is from passing a str to range

您的错误是将str传递给范围

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 

#4


I am not sure how you code looks, but it might be something like this:

我不确定你的代码看起来如何,但它可能是这样的:

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

If that is the case, remove the quotation marks "" from the number, so it looks like this.

如果是这种情况,请从数字中删除引号“”,因此它看起来像这样。

var1 = 33 #Quotation marks removed
for i in range(var1):
    print(i)

#1


Cast the user input. Then you will get an int instead of a string:

投射用户输入。然后你会得到一个int而不是一个字符串:

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

noforms = int(输入(“你要搜索多少种形式”))

#2


In python3 use: noforms = int(input("tldr")) so You will get value as a int, not a string

在python3中使用:noforms = int(输入(“tldr”))所以你将获得值作为int,而不是字符串

#3


Cast the input to an int. input returns a string in python3.

将输入转换为int。 input在python3中返回一个字符串。

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

Your error is from passing a str to range

您的错误是将str传递给范围

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 

#4


I am not sure how you code looks, but it might be something like this:

我不确定你的代码看起来如何,但它可能是这样的:

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

If that is the case, remove the quotation marks "" from the number, so it looks like this.

如果是这种情况,请从数字中删除引号“”,因此它看起来像这样。

var1 = 33 #Quotation marks removed
for i in range(var1):
    print(i)