Search code examples
pythoninteger

How would I increase a integer value inside this loop?


Trying to increase an integer value

So basically I'm trying to increase "e" so everytime I input a new line it would go 0, 1 etc

My input would look like this

example1
example2
def no(paths: str):
    e = 0
    for path in paths.split(" "):
        print("            ItemListData(" + str(e) + ")=(ItemDefination=" + path + '")')
        e = e + 1

while True:
    paths = input()
    no(paths)

When I do this I get this:

ItemListData(0)=(ItemDefination=example1")
ItemListData(0)=(ItemDefination=example2")

What I want is this

ItemListData(0)=(ItemDefination=example1")
ItemListData(1)=(ItemDefination=example2")

Solution

  • Assuming you want to have the value e increase every time you execute the function, you can do this:

    def no(paths: str, e: int):
        for path in paths.split(" "):
            print("            ItemListData(" + str(e) + ")=(ItemDefination=" + path + '")')
    
    e = 0
    while True:
        paths = input()
        no(paths, e)
        e += 1
    

    The first two lines of output, with inputs as follows:

    execute1 (enter)
    execute2
    
    execute1
        ItemListData(0)=(ItemDefination=execute1")
    execute2
        ItemListData(1)=(ItemDefination=execute2")
    

    Note that you have an infinite loop. To not have an infinite loop, you could change the loop condition to something like while e < x to make the loop contents execute x times.