如何在python中为for添加值而没有明确的数据



我想在参数中添加值

https://example.com/id=' union select (Here))

但我不知道怎么做?

this is my for:

number_of_columns = 3
for num in number_of_columns :
num = "a"
url = url + "' " + "UNION SELECT " + num 

我想当我改变number_of_columns时它会添加我设置的个数

如果我正确理解了你的问题,你可以使用input()

>>> number_of_columns = int(input("Enter the number of columns: "))
Enter the number of columns: 5

之后,您需要将URL变量声明为空字符串。从你的代码中,我注意到你没有这样做。如果你不这样做,并尝试在循环中创建一个字符串,那么你将得到一个NameError: name 'url' is not defined错误。因此,接下来执行以下操作-

url = 'example_url'

现在,最后,你的for循环有一个问题,因为你试图迭代一个int变量。这不会工作,你会得到一个错误,像这样-TypeError: 'int' object is not iterable。要纠正这个问题,可以使用range()函数。

所以你的for循环实现可以如下所示-

>>> for num in range(number_of_columns):
num = "a"
url = url+"'"+"UNION SELECT "+num
print(URL)

输出为

example_url'UNION SELECT a
example_url'UNION SELECT a'UNION SELECT a
example_url'UNION SELECT a'UNION SELECT a'UNION SELECT a
example_url'UNION SELECT a'UNION SELECT a'UNION SELECT a'UNION SELECT a
example_url'UNION SELECT a'UNION SELECT a'UNION SELECT a'UNION SELECT a'UNION SELECT a

如果您想知道为什么输出是这样的,那是因为在循环的每次迭代中,url变量都从添加到它的前一次迭代中获取信息。如果不希望发生这种情况,可以考虑编写如下所示的代码——

>>> number_of_columns = int(input("Enter the number of columns: "))
Enter the number of columns: 5
>>> url = 'example_url'
>>> for num in range(number_of_columns):
temp = ''
num = "a"
temp = url+"'"+"UNION SELECT "+num
print(temp)

example_url'UNION SELECT a
example_url'UNION SELECT a
example_url'UNION SELECT a
example_url'UNION SELECT a
example_url'UNION SELECT a

最新更新