[Tutor] Python Programming for the absolute beginner 3e Ch3 Challenge 1
alan.gauld at yahoo.co.uk
Wed Jul 27 05:13:50 EDT 2016
On 27/07/16 02:39, kanishk srivastava wrote:
> I am working through Michael Dawson's book - Python Programming for
> absolute beginners 3e.
> Completed chapter 3, but unable to solve challenge 1.
I don't know the book so don't know how much you
know yet. So thee are some suggestions below that
might not make sense to you because you haven't
covered the material. Apologies in advance!
> print("\t\tFortune Cookie")
> print("\t\tWelcome user!")
> # fortune messages
> message1 = "the earth is a school learn in it."
> message2 = "be calm when confronting an emergency crisis."
> message3 = "you never hesitate to tackle the most difficult problems."
> message4 = "hard words break no bones, fine words butter no parsnips."
> message5 = "Make all you can, save all you can, give all you can."
Have you covered lists? Rather than a set of numbered messages you could
put them in a list and access each message by index:
messages = ["the earth is a school learn in it.",
"be calm when confronting an emergency crisis.",
"you never hesitate to tackle the most difficult problems.",
"hard words break no bones, fine words butter no parsnips.",
"Make all you can, save all you can, give all you can."
And access them with
messages, messages, ... messages
Although in this case you don't even need to do that,
> import random
I don't know how many of the random module functions you cover
but there are a lot of them. One of them, random.choice(aList),
selects a random item from a list. So now that you have a list
of messages it becomes trivial to select a random message from
> print("Your today's fortune ")
use print(random.choice(messages)) here
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor