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
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>"]
where i
changes value during each phase of the loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.