By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

How to print random strings

P: n/a
Im at the end of chapter 3 of "Python Programming For The Absolute
Beginner, Michael Dawson " and he asks to make a fortune program that
displays a fortune each time its ran, and to have 5 unique fortunes.

Whats confusing is that, he never discussed how to do this. The only
thing he talked about was using random.randrange() and I tried that
with text but it seems like its only for integers as it complains when
I put text in the argument.

So how would I go about have 5 strings, and running a program that will
randomly pick one of those to print?

I think he may have forgot to cover something?

Nov 3 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
th***********@gmail.com writes:
Im at the end of chapter 3 of "Python Programming For The Absolute
Beginner, Michael Dawson " and he asks to make a fortune program that
displays a fortune each time its ran, and to have 5 unique fortunes.

Whats confusing is that, he never discussed how to do this. The only
thing he talked about was using random.randrange() and I tried that
with text but it seems like its only for integers as it complains when
I put text in the argument.

So how would I go about have 5 strings, and running a program that will
randomly pick one of those to print?

I think he may have forgot to cover something?


How about using the integer as an index to access the elements of a list? ;-)

--
Jorge Godoy <go***@ieee.org>
Nov 3 '05 #2

P: n/a
th***********@gmail.com writes:
Im at the end of chapter 3 of "Python Programming For The Absolute
Beginner, Michael Dawson " and he asks to make a fortune program that
displays a fortune each time its ran, and to have 5 unique fortunes.

Whats confusing is that, he never discussed how to do this. The only
thing he talked about was using random.randrange() and I tried that
with text but it seems like its only for integers as it complains when
I put text in the argument.

So how would I go about have 5 strings, and running a program that will
randomly pick one of those to print?

I think he may have forgot to cover something?


Well, randrange can be used to do this, but random.choice is more
pythonic.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 3 '05 #3

P: n/a
th***********@gmail.com wrote:
So how would I go about have 5 strings, and running a program that will
randomly pick one of those to print?

import random
text = "Perhaps reading the manual is a good idea?".split()
random.choice(text) 'is' random.choice(text) 'reading' random.choice(text) 'a' random.choice(text) 'the' random.choice(text)

'Perhaps'

See http://docs.python.org/lib/module-random.html
Nov 3 '05 #4

P: n/a
nak
th***********@gmail.com wrote:
Im at the end of chapter 3 of "Python Programming For The Absolute
Beginner, Michael Dawson " and he asks to make a fortune program that
displays a fortune each time its ran, and to have 5 unique fortunes.

Whats confusing is that, he never discussed how to do this. The only
thing he talked about was using random.randrange() and I tried that
with text but it seems like its only for integers as it complains when
I put text in the argument.

So how would I go about have 5 strings, and running a program that will
randomly pick one of those to print?

I think he may have forgot to cover something?


The fortune cookie program can be made by copying the code for 'Mood
Computer' on page 64 and 65. So it could be something like:

import random
print "Welcome to the Fortune cookie program"
fortune = random.randrange(3)
if fortune == 0:
print "some fortune1"
elif fortune == 1:
print "some fortune2"
elif fortune == 2:
print "some fortune3"

Nov 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.