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? 4 1889 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 > 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. 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 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" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 typing
them. So I need to know what is the value of x.
|
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)
|
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.
|
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.
|
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 helps to understand what I'm trying to do I have the following
python code.
| |
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
|
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()
|
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
|
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));
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |