[Tutor] Quote of the Day version 1.0

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Aug 31 21:48:29 CEST 2011


-----Original Message-----
I made some headway on the quote of the day program. I just tried to
do it simple using two assumptions:

- long quotes are going to print funny until I figure out the string
splitting stuff
- hard coding the quotes and authors in the program is simple, and I
can use a spreadsheet to generate the strings when I put in the rest
of the quotes (I have a lot of them).

This code works. Now I just have to figure out:

- how to get Ubuntu to run it at startup
- how to associate .py files in Ubuntu to IDLE

Thanks for all who commented, I really appreciate it. Pyhton is fun!





# Quote_a_day_V1.py
#
# This program displays a random quotation
#
# Frank L. Palmeri August 31, 2011

import random

print("\n\tHere is today's quote of the day:\n")

author = (
    "Kahlil Gibran",
    "Henrik Ibsen",
    "Dwight Eisenhower",
    "Will Rogers",
    "Will Rogers",
    "Jean de LaFontaine",
    "Eleanor Hibbert",
    "Baruch Spinoza",
    "Albert Camus",
    "Thomas a Kempi"
    )

quotation = (
    "A candle loses nothing of its light when lighting another.",
    "The strongest man in the world is he who stands most alone.",
    "Leadership consists of nothing but taking responsibility for
everything that goes wrong and giving your subordinates credit for
everything that goes well.",
    "Even if you're on the right track, you'll get run over if you
just sit there.",
    "I belong to no organized party. I am a Democrat.",
    "Patience and time do more than strength or passion.",
    "Never regret. If it's good, it's wonderful. If it's bad, it's experience.",
    "I have made a ceaseless effort not to ridicule, not to bewail,
not to scorn human actions, but to understand them.",
    "In the depth of winter I finally learned there was in me an
invincible summer.",
    "Who has a harder fight than he who is striving to overcome himself."
    )

numAuthors = len(author)

printAuthor = random.randrange(numAuthors)

print(quotation[printAuthor])

print("by ", author[printAuthor])

input("\nPress the enter key to exit.")
--------------------------------------------------------

I would suggest storing the authors and quotes in a list[] instead of a tuple(). This will allow you make modifications directly to the list. Lists are mutable (changeable) while tuples are not. This will leave you in better shape if you decide to use pickle/shelve to store the lists in a file for reuse.

# tuple example 
>>> t = (1,2,3)
>>> t
(1, 2, 3)
>>> t[0]
1
>>> t[0] = 5
TypeError: 'tuple' object does not support item assignment

# list example
>>> l = [1,2,3]
>>> l
[1, 2, 3]
>>> l[0]
1
>>> l[0] = 5
>>> l[0]
5
>>> l.append( 2100 ) # allows you to easily add new elements
>>> l
[5, 2, 3, 2100]

As for string splitting, storing the full string as you are is the best option. You should leave your client (commandline/text, web, GUI) to decide what is appropriate for splitting and where it should be split.


Although, if you want my opinion, I would probably create a quote class that stores some information. This way it is easier to make modifications and additions to the capabilities of the Quote. Not to mention this bundles the information together and you never have to worry about your author list and quote list getting out of sync. And if you derive Quote from object you *should* be able to pickle the list of Quotes very easily.


(The following is untested).

class Quote(object):
    def __init__(self, quote, author, date=None, misc=None):
        self.quote = quote
        .....



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list