You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>The function <code>is_friend_of_harry()</code> returns a <em>True</em> value if a given name (i.e. type string) is one of the friends of Harry Potter, otherwise the function returns <em>False</em>. Let's pretend the friends of Harry Potter are: <em>"Ron", "Hermione", "Hagrid",</em> and <em>"Dumbledore"</em>. </p>
319
335
<p><strong>Example:</strong> calling the function <code>is_friend_of_harry("Malfoy")</code> returns <em>False</em>.</p>
320
-
<p><spanclass="ex-part"><strong>1.a)</strong> Define the function <code>is_friend_of_harry()</code>, and call it using the following names as parameters: <em>"Hagrid", "Voldemort",</em> and <em>"Bellatrix"</em>. Print the result (i.e. <em>True/False</em>) of each call. </span></p>
336
+
<p><spanclass="ex-part"><strong>1.a)</strong> Define the function <code>is_friend_of_harry()</code>, and call it for each one of the following characters: <em>"Hagrid", "Voldemort",</em> and <em>"Bellatrix"</em>. Print the result (i.e. <em>True/False</em>) of each call. </span></p>
<p><spanclass="ex-part"><strong>1.c)</strong> Let's make our function <code>is_friend_of_harry()</code> a bit more powerful. Such that if I type a name in lowercase (e.g. "ron") or if I use spaces at the end of the name (e.g. "Ron "), or even if I do both the things, the function should work the same. </p>
360
+
<p><spanclass="ex-part"><strong>1.c)</strong> Let's make our function <code>is_friend_of_harry()</code> a bit more powerful. The new function should work the same way even if the given name is not well-written: (a) is all/in part written in lowercase (e.g. "ron", "Ron"), (b) includes spaces (e.g. "Ron ", " ron"), or (c) has both (a) and (b) cases in it. </p>
345
361
<p><strong>Hint:</strong> in python <code>{string}.lower()</code> transforms a string to its lowercase form; <code>{string}.strip()</code> removes the starting and ending whitespaces (if any); and <code>{string}.capitalize()</code> capitalizes the first letter of a string </span></p>
<p><spanclass="ex-part"><strong>1.d)</strong> Define another function <code>is_prof_friend_of_harry()</code> which returns a <em>True</em> value if a given nameis a professor and a friend of Harry, otherwise the function must return <em>False</em>. The function should call the improved version of the function <code>is_friend_of_harry()</code>in its code (i.e. defined in (1.c)). Let's pretend the professors of Harry Potter are: <em>"Snape", "Lupin", "Hagrid",</em> and <em>"Dumbledore"</em>. </span></p>
373
+
<p><spanclass="ex-part"><strong>1.d)</strong> Define another function <code>is_prof_friend_of_harry()</code> which returns a <em>True</em> value if a given name, which might be in a not well-written form (as in (1.c)), is a professor and a friend of Harry, otherwise the function must return <em>False</em>. The function should call the improved version of the function <code>is_friend_of_harry()</code> (i.e. defined in (1.c)) in its code. Let's pretend the professors of Harry Potter are: <em>"Snape", "Lupin", "Hagrid",</em> and <em>"Dumbledore"</em>. </span></p>
<p>Each house of Hogwarts has a score which is updated based on some actions such as exams grades and the rules violation. Let's pretend we have a list of the houses <codeclass="py">houses = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]</code> and another list containing the score of each house <codeclass="py">scores = [0,0,0,0]</code>, such that the score value in position N of the scores list belongs to the house in position N of the houses list .</p>
376
-
<p><strong>a)</strong> Define the function <code>update_house_score()</code> function which increments/decrements the score of a specific house with a given points. The function takes three parameters: <code>house_name</code>, <code>action</code> (string value "+"/"-"), and <code>points</code>.<br/>
392
+
<p><strong>2.a)</strong> Define the function <code>update_house_score()</code> function which increments/decrements the score of a specific house with a given points. The function takes three parameters: <code>house_name</code>, <code>action</code> (string value "+"/"-"), and <code>points</code>.<br/>
377
393
<strong>Example:</strong> Calling the function this way: <codeclass="py">update_house_score("Gryffindor","+",5)</code> should increment the score of house Gryffindor by 5 points.<br/>
378
394
<strong>Hint:</strong> The function <codeclass="py">{list}.index({value})</code> returns the index of an element in the list, e.g. <codeclass="py">houses.index("Hufflepuff")</code> returns the value <em>1</em></p>
<p><strong>b)</strong> After the Quidditch cup the Houses have increased/decreased their scores as follows: +10 Gryffindor, +7 Hufflepuff, -3 Slytherin. After the game a member of house "Slytherin" helped a man riding his broom and his house gained back 5 points. Call the function <code>update_house_score()</code> for each action in order to update the houses points and print the two lists: <code>houses</code> and <code>scores</code>.</p>
406
+
<p><strong>2.b)</strong> After the Quidditch cup the Houses have increased/decreased their scores as follows: +10 Gryffindor, +7 Hufflepuff, -3 Slytherin. After the game a member of house "Slytherin" helped a man riding his broom and his house gained back 5 points. Call the function <code>update_house_score()</code> for each action in order to update the houses points and print the two lists: <code>houses</code> and <code>scores</code>.</p>
<p><strong>2.c)</strong> redefine <code>update_house_score()</code> using the function <code>eval()</code> (<ahref="https://docs.python.org/3/library/functions.html#eval">https://docs.python.org/3/library/functions.html#eval</a>)</p>
0 commit comments