0

I am trying to automate some of the manual tasks using python3 and I am not sure how to print a variable. I know I can use print(f"https://{ip1}", but I am trying to solve this problem using "for loops"

Here is the part of the code.

......
def hostFile():
    hostConf = (projdateprojname[9:])
    print("\n\tEnter legacy host IP")
    ip1  = input(prompt)
    print("\n\tEnter legacy guest IP")
    ip2  = input(prompt)
    print("\n\tEnter legacy host IP")
    ip3  = input(prompt)
    print("\n\tEnter legacy guest IP")
    ip4  = input(prompt)
    print(f"\n[{hostConf}-1]")
    print(f"{ip1}")
    print(f"{ip2}")
    print(f"{ip3}")
    print(f"{ip4}")
    for i in range(1, 5):
        listofurl =  ("ip" + str(i))
        print(f"https://{listofurl}")
    Script output for the last part:
    https://ip1
    https://ip2
    https://ip3
    https://ip4


    I am expecting to see, for example 
    https://10.1.1.199
    https://10.1.1.200
    https://10.1.1.201
    https://10.1.1.202
Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Tash
  • 31
  • 5
  • You could change your `ip` variables into a list. – khelwood Aug 25 '21 at 23:51
  • 1
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – ggorlen Aug 25 '21 at 23:51

1 Answers1

0

I see what you were trying to do, just one adjustment change

listofurl =  ("ip" + str(i))

to

listofurl =  (locals()["ip" + str(i)])

locals() return a dictionary{} of variables in the current scope, ["ip" + str(i)] gets translated to ["ip<i>"] wherei changes value during each phase of the loop

Savvii
  • 480
  • 4
  • 9
  • 1
    Hi Dave, I had to go through the concept of locals() few time. I wasn't aware of that function. I really appreciate your help. Thanks and much obliged. – Tash Aug 26 '21 at 16:56