473,573 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.randrang e() 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 1877
th***********@g mail.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.randrang e() 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***********@g mail.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.randrang e() 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.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 3 '05 #3
th***********@g mail.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(t ext) 'is' random.choice(t ext) 'reading' random.choice(t ext) 'a' random.choice(t ext) 'the' random.choice(t ext)

'Perhaps'

See http://docs.python.org/lib/module-random.html
Nov 3 '05 #4
nak
th***********@g mail.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.randrang e() 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.randrang e(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
2568
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 question, so let's go: I want to generate 10000 strings of x characters, with one chance (or less) on a million that you can guess them by just randomly...
21
23114
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 generators, so my solution at the moment is: import string import random random.seed(14)
28
3667
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 appreciated.
7
7258
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? STL set, map? Could you give me a little code example? Thank you.
4
2988
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 following output would be ok a,b,c,a,d,c or a,c,d,a,d,a but the following would not; a,b,c,c, or a,b,b,c, etc... I hope that makes sense. If it...
4
1375
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 createRandomList(param): length = param
2
1739
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 number. """ x = 0.0 for i in range(12): x += random.random()
14
4728
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
7551
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; i<count; i++) { Random Rnd = new Random(); number = number+Convert.ToString(Rnd.Next(0,9));
0
7693
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8021
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7781
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5291
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3732
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2208
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1040
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.