How do a add a value in for with out cloear data in python

I wanna add value in parameter like :

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

but I don’t know how ?

this is my for :

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

I wanna when I change the number_of_columns it add that many I set on that

>Solution :

If I’ve understood your question correctly, you can use input()

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

After this you need to declare the URL variable as an empty string. From your code, I noticed that you haven’t done that. If you don’t do that and try to create a string inside your loop, then you will get a NameError: name 'url' is not defined error. So do the following next-

url = 'example_url'

Now finally, your for loop has a problem because you are trying to iterate through an int variable. This will not work, you will get an error like so – TypeError: 'int' object is not iterable. To rectify this, you can use the range() function.

So your for loop implementation can be as shown below –

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

This will output as

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

If you are wondering why the output looks this way, it is because in every iteration of the loop the url variable is getting information from the previous iteration added on to it. If you don’t want that to happen, you can consider writing the code as shown below –

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

Leave a Reply