Christmas and New Year Greetings in Python

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Copy and paste the code below into this box (the code is taken from here), run it to get your Christmas and New Year Greetings

  1. # Cut and paste this text into a text editor and save in a file
  2. # christmasgreetings.py
  3. # then execute it as a Python program
  4.  
  5. # AUTHOR Paul Curzon, QMUL www.cs4fn.org
  6. # This is a christmas card writing program to save all that time writing cards
  7. # This program was inspired by the 'first creative program' written by Chris Strachey,
  8. # It was a love letter writing program
  9. # The program provides a few template sentences to use.
  10. # It uses them in a random order, filling them with words from word lists
  11. # The word lists are for different grammatical categories like nouns and verbs
  12.  
  13. # Modify this program by adding new words to the word list
  14. # And adding new sentence templates
  15.  
  16. # Alternatively modify it to be a christmas card love letter
  17. # Or modify it for some other occasion
  18.  
  19. import random
  20.  
  21. # Define global constants
  22.  
  23. # This gives the number of sentences the program can choose from
  24. # Add 1 to this number for each new sentence template you add to the program
  25. NUMBER_OF_TEMPLATES = 3
  26.  
  27.  
  28. # Pick a word at random from a given word list removing it so it isn't used again
  29. def ChooseWord(wordlist) :
  30.     word = random.choice(wordlist)
  31.     wordlist.remove(word)
  32.     return word
  33.  
  34. # Pick a random number from a list of integers removing it so it isn't used again
  35. def PickRandomNumber(ns) :
  36.     n = random.choice(ns)
  37.     ns.remove(n)
  38.     return n
  39.  
  40. # Sentence Template
  41. # I wish you a ADJECTIVE NOUN.
  42. def IWishYou(adjectives, nouns) :
  43.     noun = ChooseWord(nouns)
  44.     adjective = ChooseWord(adjectives)
  45.     print("We wish you a " + adjective + " " + noun + ".")
  46.  
  47.  
  48. # Sentence Template
  49. # I hope VERB you ADVERB.
  50. def IHopeTo(verbs, adverbs) :
  51.     verb = ChooseWord(verbs)
  52.     adverb = ChooseWord(adverbs)
  53.     print("We hope " + verb + " you " + adverb + ".")
  54.  
  55. # Sentence Template
  56. # Have a ADJECTIVE NOUN.   
  57. def HaveA(adjectives, nouns) :
  58.     adjective = ChooseWord(adjectives)
  59.     noun = ChooseWord(nouns)
  60.     print("Have a " + adjective + " " + noun + ".")
  61.  
  62. # Sentence Template
  63. # SALUTATION!  
  64. def Salutation(salutations) :
  65.     salutation = ChooseWord(salutations)
  66.     print()
  67.     print(salutation + "!")
  68.  
  69. # Sentence Template
  70. #  END Q   
  71. def SignOff(endings) :
  72.     end = ChooseWord(endings);
  73.     print(end);
  74.     print("TSSFL Open Discussion Forums");
  75.  
  76. # Create a christmas greeting using the template sentences and word lists below
  77. # Always start with a salutation and end with a sign off
  78. # In between choose the templates in a random order and substitute in words from
  79. # the appropriate word lists at random
  80. def GenerateChristmasGreeting() :  
  81.     salutations =  ["Happy Christmas", "Merry Christmas", "Season's Greetings", "Happy New Year"]
  82.     adjectives =   ["wonderful", "joyous", "peaceful", "relaxing", "great"]
  83.     nouns =        ["holiday", "time", "few weeks", "Christmas"]
  84.     verbs =        ["to see", "to catch up with", "to be with", "to meet up with"]
  85.     adverbs =      ["soon", "in the new year", "sometime soon", "before long"]
  86.     endings =      ["With love", "Best wishes", "Wish you a blessed 2021"]
  87.    
  88.     Salutation(salutations)
  89.    
  90.     templatesleft = list(range(0,NUMBER_OF_TEMPLATES))
  91.    
  92.     while len(templatesleft)>0 :
  93.         sentencechoice = PickRandomNumber(templatesleft)
  94.         if (sentencechoice == 0) :
  95.             IWishYou(adjectives, nouns)
  96.         elif (sentencechoice == 1) :
  97.             IHopeTo(verbs, adverbs)
  98.         elif (sentencechoice == 2) :
  99.             HaveA(adjectives, nouns)
  100.         else :
  101.             print("We're lost for words")
  102.        
  103.     SignOff(endings)
  104.  
  105. GenerateChristmasGreeting()

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 8 guests