2

I am fairly very new to Python or any programming language for that matter. I am finding it challenging to print arbitrary text along with with iterated values from a list, using for loop. What I need is to print the whole first item of the list with arbitrary text "shutdown" and repeat the same for new text "no shutdown". Also, I want to be able to insert any text statements between the two separate outputs: Please advise. Here is more information..

CURRENT OUTPUT: Which is not desired

interface Vlan100
 shut
 no shut
interface Vlan108
 shut
 no shut

EXPECTED OUTPUTS:

# First time printing:

********* THIS IS INTERFACE OUTPUT WITH "SHUTDOWN" **********
interface Vlan100
 shutdown
interface Vlan108
 shutdown

** THIS IS INTERFACE OUTPUT WITH "NO SHUTDOWN" ***

*# Second time printing:*
interface Vlan100
 no shutdown
interface Vlan108
 no shutdown

CODE EXTRACT, Snipped for brevity reasons.

for i in servicetypes:
<snip>
<snip>
elif "ipv4 address" in i or "ipv4 address 8" in i or " ipv4 address 213" in i:
    i = re.sub(r'encapsulation dot1Q \d+\n\s','',i)
    i = re.sub(r'TenGigE[0-9]/[0-9]/[0-9]/[0-9].','Vlan',i)
    internet = i.split("\n")
    print internet[1]
    print " shut"
    print " no shut"
14
  • 1
    The reasons are good, but the code contains errors. You shouldn't use any formatting tags, just use 101010 button for formatting. Commented Dec 17, 2010 at 14:08
  • 8
    If you print " shut" followed by " no shut" then of course they're both going to show. Commented Dec 17, 2010 at 14:09
  • 1
    "ipv4 address" in i or "ipv4 address 8" in i or " ipv4 address 213" in i is kind of redundant, assuming i is a string. Commented Dec 17, 2010 at 14:14
  • 1
    Thanks AndiDog, I fixed the redundancy issue but that's not relevant to issue I pointed out which is to print the output as shown in the expected output section. I can always change the code to make print the way I expect it? My apologies if I am missing something top basic here. Any suggestions? Commented Dec 17, 2010 at 14:22
  • 3
    Do you mean you want all interfaces to print "shutdown" on the first pass through the outer loop, and "no shutdown" on all subsequent passes? If not, you need to provide a better description of your intended program logic. Commented Dec 17, 2010 at 14:28

2 Answers 2

1

Well, a few mistakes. 1) Python arrays are indexed at 0. So your call to print internet[1] is printing the second element of your array. Given you claim in your description "What I need is to print the whole first item of the list", that call should be print internet[0]

Secondly, you claim that you need to print the string "shutdown" or "no shutdown"... yet you are printing "shut" and "no shut". Change those print statements to use "shutdown" and "no shutdown".

Furthermore, you need to make some conditional to determine whether to print the string "shutdown" or "no shutdown"; right now you are printing both.

Sign up to request clarification or add additional context in comments.

6 Comments

I82Much - Thank you for pointing that out as I composed this email in rush. Consider the code where you pointed out as errors in the code and here's the correct one if you like: It's the second value of list which I have no issues in printing the issue is with printing "shutdown" and "no shutdown".. print internet[1] print " shutdown" print " no shutdown"
email? Computer programming is an art that requires you to be exact. Try to not call questions emails. :)
Never mind!! It's my 3rd day using/learning any computer language and I am able to write or atleast achieve some decent results . I don't even consider my self programmer or an artist. Apologies if I offended you programming gods here. Hoping to see some sort of support here. Also, never knew calling questions as emails is illegal here :) atleast in the world of programming.. Thanks for your time though.. Cheers
@user531942: "Also, never knew calling questions as emails is illegal here". It's not illegal. It's ill-advised. These are not "emails". These are "questions". Exact matters a lot. A real lot. Please try to be really exact all the time, or you'll never learn to program.
Trust me, I am trying.. So far, I have found it fun and need to stay motivated with help of gurus like yourself :-) Cheers
|
1

Currently you do this:

print " shut"
print " no shut"

The result of that is:

shut
no shut

As expected. If you want it to print "shut" when it shuts down and "no shut" when it doesn't, you need a test:

if shutdown(interface):
    print "shut"
else:
    print "no shut"

Or something similar.


(Edit: OK, you answered that, you want the above, not the below, so ignore this part.

If however, you just want to print

interface Vlan100
 shut
interface Vlan108
 shut
interface Vlan100
 no shut
interface Vlan108
 no shut

Then you need two separate loops.

It's not exactly clear what you want or what you are trying to do, though.)

OK. here is how to get that output:

for what in (" shut", " no shut"):
    for iface in ('vlan100', 'vlan108'):
         print "interface", iface
         print what

5 Comments

Thanks Lennart - I wanted the later part. interface Vlan100 shut interface Vlan108 shut interface Vlan100 no shut interface Vlan108 no shut
Can I know what the code is going to look like to achieve the following? I don't any conditional printing here.. Just the following output.. interface Vlan100 shut interface Vlan108 shut interface Vlan100 no shut interface Vlan108 no shut
Thanks Lennart for the code, but I am already in a for loop and this is not a new code so it's creating many iterations. Using your code I am getting the following. How can I post the full code here? besides, interface Vlan100 and interface Vlan800 should come from list instead through iteration. Here's what I am getting interface vlan100 shut interface vlan108 shut interface vlan100 no shut interface vlan108 no shut interface vlan100 shut interface vlan108 shut interface vlan100 no shut interface vlan108 no shut ..... So on!!
You can put the fill code on a pastebin site and link. They come from a list (or well tuple) that is iterated over. I'm sorry I'm not going to be able to explain this more carefully to you, you need to go through more tutorials/books to understand the fundamentals of programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.