Advertisement
Guest User

ex26_My_Code

a guest
Nov 1st, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. def break_words(stuff):
  2.     """This function will break up words for us."""
  3.     words = stuff.split()
  4.     return words
  5.  
  6. def sort_words(words):
  7.     """Sorts the words."""
  8.     return sorted(words)
  9.  
  10. def print_first_word(words):
  11.     """Prints the first word after popping it off."""
  12.     word = words.pop(0)
  13.     print(word)
  14.  
  15. def print_last_word(words):
  16.     """Prints the last word after popping it off."""
  17.     word = words.pop(-1)
  18.     print(word)
  19.  
  20. def sort_sentence(sentence):
  21.     """Takes in a full sentence and returns the sorted words."""
  22.     words = break_words(sentence)
  23.     return sort_words(words)
  24.  
  25. def print_first_and_last(sentence):
  26.     """Prints the first and last words of the sentence."""
  27.     words = break_words(sentence)
  28.     print_first_word(words)
  29.     print_last_word(words)
  30.  
  31. def print_first_and_last_sorted(sentence):
  32.     """Sorts the words then prints the first and last one."""
  33.     words = sort_sentence(sentence)
  34.     print_first_word(words)
  35.     print_last_word(words)
  36.  
  37.  
  38. print("Let's practice everything.")
  39. print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t'
  40.       ' tabs.')
  41.  
  42. poem = """
  43. \tThe lovely world
  44. with logic so firmly planted
  45. cannot discern \n the needs of love
  46. nor comprehend passion from intuition
  47. and requires an explanation
  48. \n\t\twhere there is none.
  49. """
  50.  
  51.  
  52. print("--------------")
  53. print(poem)
  54. print("--------------")
  55.  
  56. five = (10 * 2) - (3 * 5)
  57. print("This should be five: {}".format(five))
  58.  
  59. def secret_formula(started):
  60.     jelly_beans = started * 500
  61.     jars = jelly_beans / 1000
  62.     crates = jars / 100
  63.     return jelly_beans, jars, crates
  64.  
  65.  
  66. start_point = 10000
  67. beans, jars, crates = secret_formula(start_point)
  68.  
  69. print("With a starting point of: {}".format(start_point))
  70. print("We'd have {} jeans, {} jars, and {} crates.".format(beans, jars, crates
  71. ))
  72.  
  73. start_point /=  10
  74.  
  75. print("We can also do that this way:")
  76. print("We'd have {} beans, {} jars, and {} crates".format(secret_formula(start_point)))
  77.  
  78.  
  79. sentence = "All good\tthings come to those who wait."
  80.  
  81. words = break_words(sentence)
  82. sorted_words = sort_words(words)
  83.  
  84. print_first_word(words)
  85. print_last_word(words)
  86. print_first_word(sorted_words)
  87. print_last_word(sorted_words)
  88. sorted_words = sort_sentence(sentence)
  89. print(sorted_words)
  90.  
  91. print_first_and_last(sentence)
  92.  
  93. print_first_and_last_sorted(sentence)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement