473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to print random strings

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
4 1872
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I've got two random number/statistics questions I'd like you to review. My first question is not directly related to PHP, but will be implemented in PHP, as explained in my second...
21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
4
by: mj.clift | last post by:
Hi All, I need to be able to choose a random string from an array. That is easy enough, but I want to restrict the repetition of that string until one or two other choices have been made. So the...
4
by: benjamin.cordes | last post by:
Hi, I have a small problem with my function: printList. I use print with a ',' . Somehow the last digit of the last number isn't printed. I wonder why. import random def...
2
by: Steven D'Aprano | last post by:
I'm working on some functions that, essentially, return randomly generated strings. Here's a basic example: def rstr(): """Return a random string based on a pseudo normally-distributed random...
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.