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 5 8973
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: p s |
last post by:
hi all
i have a vb6 project, one of the functions is to read in a text file and
place it into an array
problem is, when people use the TAB key in the original file that is read, i
just get the...
|
by: Jay Chan |
last post by:
I am trying to export data from a SQLServer database into a text file
using a stored procedure. I want to be able to read it and debug it
easily; therefore, I want all the columns to indent nicely....
|
by: bearophileHUGS |
last post by:
Hello, I know this topic was discussed a *lot* in the past, sorry if it
bores you...
>From the Daily Python-URL I've seen this interesting Floating Point
Benchmark:...
|
by: gopal srinivasan |
last post by:
Hi,
I have a text like this -
"This is a message containing tabs and white spaces"
Now this text contains tabs and white spaces. I want remove the tabs
and white...
|
by: Magix |
last post by:
Hi,
Everytime I received a fix-length of string, let say 15 (the unused portion
will filled with Spaces before receive), I want to remove the Spaces from
END until I encounter a non-space char....
|
by: lino |
last post by:
Hello,
I have the following string:
const char plaintext = "Fourscore and seven years ago our \
fathers brought forth upon this continent \
a new nation, conceived in liberty, and dedicated...
|
by: Scott |
last post by:
I wish to create a text document (to the users desktop on any machine) which outputs to a standard text file with information obtained from there use of my program... Some of the text will be fixed...
|
by: mjteigen |
last post by:
I'm very new at Python, but have been trying it in conjunction with
CGI. I've encountered a problem that I'm pretty sure is a trivial one,
but I don't know enough Python to work it out. Here's an...
|
by: Xah Lee |
last post by:
Tabs versus Spaces in Source Code
Xah Lee, 2006-05-13
In coding a computer program, there's often the choices of tabs or
spaces for code indentation. There is a large amount of confusion about...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |