From 1ddb70a4ceea2a15fa5e5b524c554c420f32828c Mon Sep 17 00:00:00 2001 From: Nicolas Bricknell Date: Sat, 7 Mar 2015 14:28:47 +0000 Subject: [PATCH] Reinstate fruits --- tex/python-workshop-2.tex | 44 ++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/tex/python-workshop-2.tex b/tex/python-workshop-2.tex index f5ae4bc..b7d1a95 100644 --- a/tex/python-workshop-2.tex +++ b/tex/python-workshop-2.tex @@ -485,32 +485,24 @@ \end{frame} \begin{frame}[fragile] - \frametitle{More keywords: continue, pass} - \begin{itemize} - \item \lstinline|pass| is just a piece of code to say do nothing. - \item When we only have a \lstinline|if| block, - \begin{lstlisting}[xleftmargin=\dimexpr-\leftmargini] -if (cond): - print(x) - \end{lstlisting} - \item it is equivalent to the following - - \begin{lstlisting}[xleftmargin=\dimexpr-\leftmargini] -if (cond): - print(x) -else: - pass - \end{lstlisting} - \pause - \item \lstinline|continue| is a way of going to the next iteration of the loop - \begin{lstlisting}[xleftmargin=\dimexpr-\leftmargini] -for num in range(10): - if (num == 5): - continue - print(num) - \end{lstlisting} - \item This prints all number except 5. If we had used \lstinline|pass| instead, then we would have printed 5 as well. Can you see why? - \end{itemize} + \frametitle{Continue} + \begin{lstlisting} + story = """ + John brought 3 apples and Jenny brought + 26 pears. Meanwhile, Bastian brought 22 + guavas! + """ + + number_of_fruits = 0 + + for word in story.split(): + try: + number_of_fruits += int(word) + except ValueError: + continue + + print(number_of_fruits, "lovely fruits!") + \end{lstlisting} \end{frame} \begin{frame}[fragile]