Skip to content

Commit d487647

Browse files
committed
Part 3 cupcake tally rewrite.
1 parent 7ca3c02 commit d487647

File tree

1 file changed

+114
-31
lines changed

1 file changed

+114
-31
lines changed

part-3.ipynb

Lines changed: 114 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"name": "python2"
1111
},
1212
"name": "",
13-
"signature": "sha256:71c98cee8fe96271cc8983aae5b8d91c4d35188a06dd6e0fce00a96d7bd13e92"
13+
"signature": "sha256:ee94c8f65b24aca6551d49f1d80ebf2b4fae52fbbda61e090166f59ccddb81b8"
1414
},
1515
"nbformat": 3,
1616
"nbformat_minor": 0,
@@ -647,81 +647,151 @@
647647
"level": 2,
648648
"metadata": {},
649649
"source": [
650-
"Example: Cupcake Count"
650+
"Example: Cupcake Tally"
651651
]
652652
},
653653
{
654654
"cell_type": "markdown",
655655
"metadata": {},
656656
"source": [
657-
"Okay, we've explored functions that:\n",
657+
"So far we've explored functions that:\n",
658658
"* Take strings or other values as input\n",
659-
"* Print strings\n",
660-
"\n",
661-
"Now, let's create a function that:\n",
662-
"* Takes a number as input\n",
663-
"* Returns a number"
659+
"* Print strings"
664660
]
665661
},
666662
{
667663
"cell_type": "markdown",
668664
"metadata": {},
669665
"source": [
670-
"So let's create a new function, `cupcake_count`. It will return a value to where the function was initially called.\n",
666+
"Let's create a new function, `cupcake_tally()`. This function will be different from the others that I covered. Rather than just printing stuff, it'll return a value when called.\n",
671667
"\n",
672-
"We'll multiply the number of guests by two and return the result because each guest wants to eat two cupcakes."
668+
"A good birthday party should allocate:\n",
669+
"* 2 cupcakes per guest\n",
670+
"* 13 cupcakes for the birthday person, because if it's your birthday, you should be allowed to eat a baker's dozen of cupcakes."
673671
]
674672
},
675673
{
676674
"cell_type": "code",
677675
"collapsed": false,
678676
"input": [
679-
"def cupcake_count(guests):\n",
677+
"def cupcake_tally(guests):\n",
680678
" \"\"\" Given number of party guests, returns how many cupcakes are needed. \"\"\"\n",
681-
" return 2 * guests"
679+
" return 2 * guests + 13"
682680
],
683681
"language": "python",
684682
"metadata": {},
685683
"outputs": [],
686-
"prompt_number": 58
684+
"prompt_number": 64
687685
},
688686
{
689-
"cell_type": "heading",
690-
"level": 1,
687+
"cell_type": "markdown",
691688
"metadata": {},
692689
"source": [
693-
"print(\"We need to make \" + str(cupcake_count(10)) + \" cupcakes.\")"
690+
"How many cupcakes do we need for a party with 30 guests?"
694691
]
695692
},
693+
{
694+
"cell_type": "code",
695+
"collapsed": false,
696+
"input": [
697+
"cupcake_tally(30)"
698+
],
699+
"language": "python",
700+
"metadata": {},
701+
"outputs": [
702+
{
703+
"metadata": {},
704+
"output_type": "pyout",
705+
"prompt_number": 65,
706+
"text": [
707+
"73"
708+
]
709+
}
710+
],
711+
"prompt_number": 65
712+
},
696713
{
697714
"cell_type": "markdown",
698715
"metadata": {},
699716
"source": [
700-
"Ok, so we call our function, get_cupcake_count, and pass in the integer 10 as the value of the parameter, 'guests'. \n",
701-
"\n",
702-
"Then we multiply the number of guests by 2 and return the result back to where the function was initially called. \n",
703-
"\n",
704-
"Lastly, it prints out the returned integer value of 20."
717+
"What about if I'm celebrating my birthday at home with just my husband, Daniel?"
705718
]
706719
},
720+
{
721+
"cell_type": "code",
722+
"collapsed": false,
723+
"input": [
724+
"cupcake_tally(guests=1)"
725+
],
726+
"language": "python",
727+
"metadata": {},
728+
"outputs": [
729+
{
730+
"metadata": {},
731+
"output_type": "pyout",
732+
"prompt_number": 69,
733+
"text": [
734+
"15"
735+
]
736+
}
737+
],
738+
"prompt_number": 69
739+
},
707740
{
708741
"cell_type": "markdown",
709742
"metadata": {},
710743
"source": [
711-
"Note if you are wondering what the str() function is... Python does not allow us to concatenate strings with integers and will tell you so. \n",
744+
"Ooh, 15 cupcakes sounds about right for our little party, yeah? ;)\n",
712745
"\n",
713-
"We need to convert the integer with the string function to convert the integer into a string object"
746+
"Notice how we explicitly named the `guests` parameter in that example."
747+
]
748+
},
749+
{
750+
"cell_type": "heading",
751+
"level": 3,
752+
"metadata": {},
753+
"source": [
754+
"Printing the return value"
714755
]
715756
},
716757
{
717758
"cell_type": "code",
718759
"collapsed": false,
719760
"input": [
720-
"print(\"We need to make \" + str(get_cupcake_count(10)) + \" cupcakes.\")"
761+
"cupcakes = cupcake_tally(10)\n",
762+
"print(\"We need to make {0} cupcakes.\".format(cupcakes))"
721763
],
722764
"language": "python",
723765
"metadata": {},
724-
"outputs": []
766+
"outputs": [
767+
{
768+
"output_type": "stream",
769+
"stream": "stdout",
770+
"text": [
771+
"We need to make 33 cupcakes.\n"
772+
]
773+
}
774+
],
775+
"prompt_number": 70
776+
},
777+
{
778+
"cell_type": "markdown",
779+
"metadata": {},
780+
"source": [
781+
"We call our function, `cupcake_tally()`, and pass in 10 as the value of `guests`. \n",
782+
"\n",
783+
"`cupcake_tally(10)` returns 33, which gets stored in the `cupcakes` variable.\n",
784+
"\n",
785+
"Lastly, we print \"We need to make 33 cupcakes.\" using Python string formatting."
786+
]
787+
},
788+
{
789+
"cell_type": "heading",
790+
"level": 3,
791+
"metadata": {},
792+
"source": [
793+
"Multiple function arguments"
794+
]
725795
},
726796
{
727797
"cell_type": "markdown",
@@ -736,28 +806,41 @@
736806
"cell_type": "code",
737807
"collapsed": false,
738808
"input": [
739-
"def get_cupcake_count(cupcakes, guests): \n",
740-
" return cupcakes * guests"
809+
"def cupcake_tally(cupcakes, guests): \n",
810+
" return cupcakes * guests + 13"
741811
],
742812
"language": "python",
743813
"metadata": {},
744-
"outputs": []
814+
"outputs": [],
815+
"prompt_number": 71
745816
},
746817
{
747818
"cell_type": "code",
748819
"collapsed": false,
749820
"input": [
750-
"print(get_cupcake_count(4, 15))"
821+
"cupcake_tally(4, 15)"
751822
],
752823
"language": "python",
753824
"metadata": {},
754-
"outputs": []
825+
"outputs": [
826+
{
827+
"metadata": {},
828+
"output_type": "pyout",
829+
"prompt_number": 73,
830+
"text": [
831+
"73"
832+
]
833+
}
834+
],
835+
"prompt_number": 73
755836
},
756837
{
757838
"cell_type": "markdown",
758839
"metadata": {},
759840
"source": [
760-
"Note that we have a 'cupcakes' parameter as well as a 'guests' parameter. Our function is called and passes in two integer values of 4 and 15. It then prints out the returned result, 60."
841+
"Note that we have 2 parameters now: `cupcakes` and `guests`\n",
842+
"\n",
843+
"Our function is called and passes in two values of 4 and 15. It returns 73."
761844
]
762845
},
763846
{

0 commit comments

Comments
 (0)