|
Hi, all.
I'm YAPN (Yet Another Python Newbie), started messing with it last night
and so far, so good. Documentation exellent and the people seem friendly
enough ;)
Ok, I started playing around with random() and decided to write a script
to generate random dotted quad IP addresses, just to see if I could do it.
Everything is perfect with the glaring exeception of the output. It seems
as if when one uses the "," in a print statement, you get an empty space
in between variables and what I want there (namely, the ".").
I've tried it several different ways with no go. Here's the script in all
her cheesy glory:
import random
n = 2
while n > 0:
a = random.randint(1,254)
b = random.randint(1,254)
c = random.randint(1,254)
d = random.randint(1,254)
print a,".",b,".",c,".",d
n = n-1
The output at the moment looks like so:
179 . 72 . 138 . 272
21 . 124 . 83 . 9
When, in a perfect universe it would look like so:
179.72.138.272
21.124.83.9
Thoughts on what critically simple something I missed?
Thanks for the input!
tom | |
Share:
|
On Tue, 03 Feb 2004 02:14:18 -0600, tgiles wrote: import random n = 2 while n > 0: a = random.randint(1,254) b = random.randint(1,254) c = random.randint(1,254) d = random.randint(1,254) print a,".",b,".",c,".",d n = n-1
The output at the moment looks like so:
179 . 72 . 138 . 272 21 . 124 . 83 . 9
This looks like a good opportunity to learn about the string formatting
operator: import random for n in ( 1, 2 ):
... a = random.randint( 1, 254 )
... b = random.randint( 1, 254 )
... c = random.randint( 1, 254 )
... d = random.randint( 1, 254 )
... print "%d.%d.%d.%d" % ( a, b, c, d )
...
147.23.187.25
138.248.253.1
You can learn about many useful Python features like this by working all
the way through the Python tutorial:
<http://www.python.org/doc/tut/>
--
\ "Either he's dead or my watch has stopped." -- Groucho Marx |
`\ |
_o__) |
Ben Finney <http://bignose.squidly.org/> | | |
try this instead
while n > 0:
a = random.randint(1,254)
b = random.randint(1,254)
c = random.randint(1,254)
d = random.randint(1,254)
print str(a) + "." + str(b) + "." + str(c) + "." + str(d)
n -= 1
"tgiles" <tg****@spam.kc.rr.com> wrote in message
news:pa****************************@spam.kc.rr.com ... Hi, all.
I'm YAPN (Yet Another Python Newbie), started messing with it last night and so far, so good. Documentation exellent and the people seem friendly enough ;)
Ok, I started playing around with random() and decided to write a script to generate random dotted quad IP addresses, just to see if I could do it.
Everything is perfect with the glaring exeception of the output. It seems as if when one uses the "," in a print statement, you get an empty space in between variables and what I want there (namely, the ".").
I've tried it several different ways with no go. Here's the script in all her cheesy glory:
import random n = 2 while n > 0: a = random.randint(1,254) b = random.randint(1,254) c = random.randint(1,254) d = random.randint(1,254) print a,".",b,".",c,".",d n = n-1
The output at the moment looks like so:
179 . 72 . 138 . 272 21 . 124 . 83 . 9
When, in a perfect universe it would look like so:
179.72.138.272 21.124.83.9
Thoughts on what critically simple something I missed?
Thanks for the input!
tom | | |
tgiles wrote:
.... print a,".",b,".",c,".",d n = n-1
The output at the moment looks like so:
179 . 72 . 138 . 272 21 . 124 . 83 . 9
When, in a perfect universe it would look like so:
179.72.138.272 21.124.83.9
print is a convenience used to do quick-and-dirty output. It's focused
on that convenience rather than on pretty formatting of reports. So, if
you're into the Java way of doing things:
import sys
sys.stdout.write( repr(a))
sys.stdout.write( '.' )
sys.stdout.write( repr(b))
....
Or, you could use one of these common idioms:
# construct a string with the entire representation before printing
print ".".join( [ str(x) for x in (a,b,c,d) ] )
# or
print str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)
# or
print '%(a)s.%(b)s.%(c)s.%(d)s'%locals()
# or
print '%s.%s.%s.%s' % (a,b,c,d)
String formatting (seen in those last two examples) is a very powerful
mechanism, more focused on less regular formatting tasks, while the
first example is more appropriate for tasks processing very large
quantities of regular data. The simple addition of strings is fine for
very simple code, but tends to get a little clumsy eventually.
Welcome to the perfect universe :) ,
Mike
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder http://members.rogers.com/mcfletch/ | | |
On Tue, 03 Feb 2004 02:14:18 -0600, "tgiles" <tg****@spam.kc.rr.com>
wrote: Ok, I started playing around with random() and decided to write a script to generate random dotted quad IP addresses, just to see if I could do it.
Everything is perfect with the glaring exeception of the output. It seems as if when one uses the "," in a print statement, you get an empty space in between variables and what I want there (namely, the ".").
Mike pointed out one a common idioms that includes a couple of idioms.
To customize it for random IP addresses:
'.'.join([str(random.randint(0,255)) for i in range(4)])
The two idioms are using the string join method to build a single
string from small parts, connecting with a single value.
The other idiom is a list comprehension. In a list comprehension, you
combine an expression and a loop, sometimes with a condition. In this
example, the expression str(random.randint(0,255)) is independent of
the loop itself. The loop is used to iterate 4 times. I suppose
using range(4) to iterate 4 times could be considered an idiom also.
--dang | | |
"tgiles" <tg****@spam.kc.rr.com> writes: Everything is perfect with the glaring exeception of the output. It seems as if when one uses the "," in a print statement, you get an empty space in between variables and what I want there (namely, the ".").
Yeah, that's because of the somewhat un-Pythonic behavior of the print
statement, which inserts spaces around the printed items, and a newline
at the end.
print a,".",b,".",c,".",d
Try
print "%d.%d.%d.%d." % (a, b, c, d) | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
13 posts
views
Thread by p s |
last post: by
|
4 posts
views
Thread by Jay Chan |
last post: by
|
17 posts
views
Thread by bearophileHUGS@lycos.com |
last post: by
|
11 posts
views
Thread by gopal srinivasan |
last post: by
|
12 posts
views
Thread by Magix |
last post: by
|
3 posts
views
Thread by lino |
last post: by
|
8 posts
views
Thread by Scott |
last post: by
|
3 posts
views
Thread by mjteigen |
last post: by
|
135 posts
views
Thread by Xah Lee |
last post: by
| | | | | | | | | | |