How to print random strings

T

theboringdays

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?
 
J

Jorge Godoy

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? ;-)
 
M

Mike Meyer

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
 
N

nak

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,339
Members
48,029
Latest member
Anchorman2022

Latest Threads

Top