[Tutor] Python Programming for the absolute beginner 3e Ch3 Challenge 1

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jul 27 05:13:50 EDT 2016


On 27/07/16 02:39, kanishk srivastava wrote:
> Hello,
> 
> 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[0], messages[1], ... messages[4]

Although in this case you don't even need to do that,
read on...

> 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
the list:

> print("Your today's fortune  ")

use print(random.choice(messages)) here

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list