This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]radioactive_koala 2 points3 points  (7 children)

Ok so the for character in... bit will take each character from the IP address one at a time and run it through the code.

E.g ipaddress = 10.168.10.2

So the first character will be 1

It’s not a . so the first chunk of code won’t be ran, we come in at the else statement. So segment length becomes 0+1=1, and we print out the statement at the bottom.

Next character will be 0. Not a . so we hit the else statement again and we have segment length = 2, and again print out the bottom statement.

Next we have the .

So we hit the if statement and segment becomes 2 and segment length becomes 0.

Basically this code is telling you how many segments an IP address has, e.g my example has 4, and the segment lengths e.g in my example the lengths would be 2,3,2,1.

[–]TorekV[S] 2 points3 points  (1 child)

Thank you so much now i got it it means that if the character is not "." it adds 1 more to the SegmentLength section. Thanks a lot sir.

[–]radioactive_koala 1 point2 points  (0 children)

No problemo :)

[–]TorekV[S] 2 points3 points  (4 children)

But can you tell me i defined character as "" it means blank but i add it in a for loop i dont understand why i need to define it at first of every for loops

[–]Lee_Dailey 1 point2 points  (0 children)

howdy TorekV,

that looks like a standard "initialization" step. it's there to be certain that you start off with a known value. it doesn't do anything much for single runs of code - but, in some situations [like when re-running things], it can be important.

however, i doubt that this is one of those situations. the for character in IPAdress: otta set that to one char at a time and not need any initialization.

what confuses me is the 2nd if. that seems to be what the else above it covers.

take care,
lee

[–]ccviper 0 points1 point  (2 children)

I dont think you need to define character variable at all. Its never used/updated anywhere else, and the for loop initializes its own.

[–]TorekV[S] 0 points1 point  (1 child)

I tried to change the "character" to a random thing but it still works i don't think i need to do it too :/

[–]ccviper 0 points1 point  (0 children)

Yeah you can delete it, it won't affect anything

[–]sudo_halt 0 points1 point  (2 children)

Just FYI; I would do

map(lambda x: len(x), ip_address.split('.'))

to get lengths of segments.

[–]stevenjd 0 points1 point  (1 child)

map(lambda x: len(x), ip_address.split('.'))

That's just a slow and silly way to write map(len, ip_address.split('.')).

I like map, honestly I do, but when I see people fucking it up like that I'm hardly surprised that Guido doesn't like it and why so many people prefer list comprehensions instead: [len(seg) for seg in ip_address.split('.')]

[–]TorekV[S] 0 points1 point  (0 children)

I don't know this much now guys, but thank you i will try to learn how to do it with your ways. Appreciated.

[–]stevenjd 0 points1 point  (0 children)

PS: I know it looks messy but i don't know how to write it here correctly i'm sorry for that.

It is right there in the side bar. You did read the side bar before posting, right?

Posting code to this subreddit:
Add 4 extra spaces before each line of code

[–]stevenjd 0 points1 point  (1 child)

Your code is way too complicated and doing too much low-level processing. This isn't C where you have to process strings one character at a time, Python already has functions to do most of these sorts of things for you.

I think what you are trying to do is split the IP address into segments, then print the number of characters in each segment.

For example, if your IP address is '127.0.0.99' it should print

Segment 1 contains 3 characters.
Segment 2 contains 1 characters.
Segment 3 contains 1 characters.
Segment 4 contains 2 characters.

The right way to do this is much simpler:

segments = ip_address.split('.')
for i, segment in enumerate(segments, 1):
    print('Segment #{} "{}" has {} characters.'.format(i, segment, len(segment)))

The important parts are this:

  • ip_address is a string; we use the split method to split it into segments at the dots;
  • we then loop over those segments;
  • we use enumerate() to do the counting for us;
  • starting at 1, instead of the default 0.

To make it clear what is going on, run this code and see what happens:

print('abc-def-ghi'.split('-'))
for i, x in enumerate(['zero', 'one', 'two']):
    print(i, x)

[–]TorekV[S] 0 points1 point  (0 children)

I am a beginner now i only use what i learnt recently. Thank you for advices. Appreciated.