-1

I want to be able to increment the contents in the {} by referencing the variable 'count' as I want it to print out a different part from the list 'nodes' each time it iterates through

count = 0

while len(nodes) > count:
    print("ltm node {} \n".format(*nodes))
    count = count+1
    print("    address {} \n".format(*nodes))
    count = count+1

I cant for the life in me figure it out of find it documented anywhere

Below is the full code

nodes = [] 

node_qty = int(input("Number Of Nodes: "))
count = 1
print("Enter the node details ")

while len(nodes) < node_qty*2:
    item1 = input("Enter Node %d Name: " % count)
    nodes.append(item1)
    item2 = input("Enter Node %d IP: " % count)
    nodes.append(item2)
    count = count+1

count = 0

print("\n"+"List of nodes:")

while len(nodes) > count:
    print("ltm node {0} \n".format(*nodes))
    count = count+1
    print("    address {1} \n".format(*nodes))
    count = count+1

Below is a transcript of it in action:

> Number Of Nodes: 2 Enter the node details
> 
> Enter Node 1 Name: Server1
> 
> Enter Node 1 IP: 192.168.1.1
> 
> Enter Node 2 Name: Server2
> 
> Enter Node 2 IP: 160.10.10.1
> 
> List of nodes:  
> ltm node Server1 
> 
>     address 192.168.1.1 
> 
> ltm node Server1  
> 
>     address 192.168.1.1

At the moment it only shows the details for index 0 and 1 as it is hard coded, but given the 'count' variable increments I would like to use this variable in the curly braces in place of the fixed numbers

(only started learning python yesterday so hopefully the code isn't too sloppy :))

  • What exactly are you trying to print? Maybe you can provide an example of the output you are expecting for a given input since it's hard to understand from your question. – pvg Mar 14 '17 at 00:07

1 Answers1

1

In each input iteration you've added 2 items to your list, so, keeping your logic as-is, you'll want to also "iterate over the list with step size 2" and index your list as done in this answer:

for i in range(0,len(nodes),2):
    print("ltm node {0} \n".format(nodes[i]))
    print("    address {0} \n".format(nodes[i+1]))

note: when accessing arguments by position each print str.format() starts over beginning with the {0} index.

Or as one print statement:

print("ltm node {0} \n    address {1} \n".format(nodes[i], nodes[i+1]))   

Output:

List of nodes:
ltm node Server1 
    address 192.168.1.1 

ltm node Server2 
    address 160.10.10.1 
Community
  • 1
  • 1