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>In this exercise, we’ll practice string manipulation and slicing. You’ll work with a secret message encoded using various string operations and learn how to apply some new methods like <code>title()</code>, <code>replace()</code> and <code>count()</code>. By following a series of steps, you’ll gradually decode the message. Let’s begin with the encoded message and work through each decoding step:</p>
<divclass="sourceCode cell-code" id="cb1"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb1-1"><ahref="#cb1-1" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Decode a secret message by following a series of instructions.</span></span>
309
309
<spanid="cb1-2"><ahref="#cb1-2" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Each instruction requires you to use different operations and methods.</span></span>
310
310
<spanid="cb1-3"><ahref="#cb1-3" aria-hidden="true" tabindex="-1"></a><spanclass="co"># The encoded message is:</span></span>
@@ -327,7 +327,7 @@ <h1>Decoding secret messages with methods</h1>
327
327
<spanid="cb1-20"><ahref="#cb1-20" aria-hidden="true" tabindex="-1"></a><spanclass="co"># e) </span><spanclass="al">TODO</span><spanclass="co">: Add an exclamation mark at the end of the sentence</span></span>
328
328
<spanid="cb1-21"><ahref="#cb1-21" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span>
<spanid="cb1-23"><ahref="#cb1-23" aria-hidden="true" tabindex="-1"></a><spanclass="co"># f) </span><spanclass="al">TODO</span><spanclass="co">: Count how many times the letter 's' appears in the decoded message</span></span>
330
+
<spanid="cb1-23"><ahref="#cb1-23" aria-hidden="true" tabindex="-1"></a><spanclass="co"># f) </span><spanclass="al">TODO</span><spanclass="co">: Count how many times the letter 's' appears in the decoded message (upper and lower case)</span></span>
331
331
<spanid="cb1-24"><ahref="#cb1-24" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>In this exercise, we’ll practice using conditional statements to classify temperatures into different categories. We’ll create a program that takes a temperature input from the user and provides a classification based on the temperature range. This exercise will help you understand how to use if-elif-else statements, handle user input, and implement a simple loop for program repetition.</p>
<divclass="sourceCode cell-code" id="cb2"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb2-1"><ahref="#cb2-1" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Use if-elif-else statements to classify the temperature</span></span>
<p>In this exercise, we’ll create an interactive number guessing game for two players. This game will help you practice using loops, conditional statements, and user input handling. You’ll also learn how to implement a simple game logic and manage player turns. This exercise will reinforce your understanding of control structures and basic development concepts in Python.</p>
<divclass="sourceCode cell-code" id="cb3"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb3-1"><ahref="#cb3-1" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Create a number guessing game with 2 players. The first player is the game master and the second player is the guesser. </span></span>
374
374
<spanid="cb3-2"><ahref="#cb3-2" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Start by asking for their names. Then, ask the game master to input the secret number between 1 and 20. </span></span>
375
-
<spanid="cb3-3"><ahref="#cb3-3" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Make sure, that the number is not printed! Then, ask the player to guess the number. </span></span>
375
+
<spanid="cb3-3"><ahref="#cb3-3" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Make sure, that the number is not shown by adding at least 25 new lines after the input prompt. Then, ask the player to guess the number. </span></span>
376
376
<spanid="cb3-4"><ahref="#cb3-4" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the guess is too high or too low, provide feedback in order to help the player and let the player guess again. </span></span>
377
377
<spanid="cb3-5"><ahref="#cb3-5" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the guess is correct, congratulate the player and end the game.</span></span>
<spanid="cb3-7"><ahref="#cb3-7" aria-hidden="true" tabindex="-1"></a><spanclass="co"># a) </span><spanclass="al">TODO</span><spanclass="co">: Your task list in more detail:</span></span>
380
380
<spanid="cb3-8"><ahref="#cb3-8" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Ask for the names of the game master and the player</span></span>
381
381
<spanid="cb3-9"><ahref="#cb3-9" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Implement the main game loop using a while loop</span></span>
382
382
<spanid="cb3-10"><ahref="#cb3-10" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Ask the game master for the secret number from 1-20</span></span>
383
-
<spanid="cb3-11"><ahref="#cb3-11" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Ask the player for a guess</span></span>
384
-
<spanid="cb3-12"><ahref="#cb3-12" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Compare the guess to the secret number</span></span>
385
-
<spanid="cb3-13"><ahref="#cb3-13" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Provide feedback (too high, too low, or correct)</span></span>
386
-
<spanid="cb3-14"><ahref="#cb3-14" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Keep track of the number of guesses</span></span>
387
-
<spanid="cb3-15"><ahref="#cb3-15" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - End the game when the correct number is guessed</span></span>
388
-
<spanid="cb3-16"><ahref="#cb3-16" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Print a congratulatory message with the number of guesses taken</span></span>
389
-
<spanid="cb3-17"><ahref="#cb3-17" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span>
<spanid="cb3-19"><ahref="#cb3-19" aria-hidden="true" tabindex="-1"></a><spanclass="co"># b) </span><spanclass="al">TODO</span><spanclass="co">: Add a feature to allow both players to play again, this time switching the roles with the game master. </span></span>
392
-
<spanid="cb3-20"><ahref="#cb3-20" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the player wants to play again, the game should reset and the player should be able to play again, entering a new secret number to guess for the former game master. </span></span>
393
-
<spanid="cb3-21"><ahref="#cb3-21" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the player does not want to play again, the game should end. Hint: Make sure to inform the players about the new game master using their stated names.</span></span>
394
-
<spanid="cb3-22"><ahref="#cb3-22" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span>
<spanid="cb3-24"><ahref="#cb3-24" aria-hidden="true" tabindex="-1"></a><spanclass="co"># c) </span><spanclass="al">TODO</span><spanclass="co">: Bonus Task - Enhance the game by adding difficulty levels</span></span>
397
-
<spanid="cb3-25"><ahref="#cb3-25" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Implement three difficulty levels: Easy (1-10), Medium (1-20), and Hard (1-30)</span></span>
398
-
<spanid="cb3-26"><ahref="#cb3-26" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Ask the game master to choose a difficulty level before entering the secret number</span></span>
399
-
<spanid="cb3-27"><ahref="#cb3-27" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Adjust the range of possible numbers based on the chosen difficulty</span></span>
400
-
<spanid="cb3-28"><ahref="#cb3-28" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Provide a different number of allowed guesses for each difficulty level:</span></span>
<spanid="cb3-32"><ahref="#cb3-32" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the player doesn't guess the number within the allowed guesses, end the game and reveal the secret number</span></span>
405
-
<spanid="cb3-33"><ahref="#cb3-33" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
383
+
<spanid="cb3-11"><ahref="#cb3-11" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Hint: Use `print("\n" * 25)` to add 25 new lines</span></span>
384
+
<spanid="cb3-12"><ahref="#cb3-12" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Ask the player for a guess</span></span>
385
+
<spanid="cb3-13"><ahref="#cb3-13" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Compare the guess to the secret number</span></span>
386
+
<spanid="cb3-14"><ahref="#cb3-14" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Provide feedback (too high, too low, or correct)</span></span>
387
+
<spanid="cb3-15"><ahref="#cb3-15" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Keep track of the number of guesses</span></span>
388
+
<spanid="cb3-16"><ahref="#cb3-16" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - End the game when the correct number is guessed</span></span>
389
+
<spanid="cb3-17"><ahref="#cb3-17" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Print a congratulatory message with the number of guesses taken</span></span>
390
+
<spanid="cb3-18"><ahref="#cb3-18" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span>
<spanid="cb3-20"><ahref="#cb3-20" aria-hidden="true" tabindex="-1"></a><spanclass="co"># b) </span><spanclass="al">TODO</span><spanclass="co">: Add a feature to allow both players to play again, this time switching the roles with the game master. </span></span>
393
+
<spanid="cb3-21"><ahref="#cb3-21" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the player wants to play again, the game should reset and the player should be able to play again, entering a new secret number to guess for the former game master. </span></span>
394
+
<spanid="cb3-22"><ahref="#cb3-22" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the player does not want to play again, the game should end. Hint: Make sure to inform the players about the new game master using their stated names.</span></span>
395
+
<spanid="cb3-23"><ahref="#cb3-23" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span>
<spanid="cb3-25"><ahref="#cb3-25" aria-hidden="true" tabindex="-1"></a><spanclass="co"># c) </span><spanclass="al">TODO</span><spanclass="co">: Bonus Task - Enhance the game by adding difficulty levels</span></span>
398
+
<spanid="cb3-26"><ahref="#cb3-26" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Implement three difficulty levels: Easy (1-10), Medium (1-20), and Hard (1-30)</span></span>
399
+
<spanid="cb3-27"><ahref="#cb3-27" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Ask the game master to choose a difficulty level before entering the secret number</span></span>
400
+
<spanid="cb3-28"><ahref="#cb3-28" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Adjust the range of possible numbers based on the chosen difficulty</span></span>
401
+
<spanid="cb3-29"><ahref="#cb3-29" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Provide a different number of allowed guesses for each difficulty level:</span></span>
<spanid="cb3-33"><ahref="#cb3-33" aria-hidden="true" tabindex="-1"></a><spanclass="co"># If the player doesn't guess the number within the allowed guesses, end the game and reveal the secret number</span></span>
406
+
<spanid="cb3-34"><ahref="#cb3-34" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>In this exercise, you will practice writing small functions for various tasks. You will also practice using the <code>return</code> statement to return a value from a function and the <code>global</code> keyword to modify a global variable.</p>
<divclass="sourceCode cell-code" id="cb1"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb1-1"><ahref="#cb1-1" aria-hidden="true" tabindex="-1"></a><spanclass="co"># a) </span><spanclass="al">TODO</span><spanclass="co">: Write a function that takes two numbers as input and returns their squared sum.</span></span>
309
309
<spanid="cb1-2"><ahref="#cb1-2" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span>
<p>In this exercise, you will practice creating different classes for different tasks. You will create a class for a bank account, a class for a car, and a class for a computer.</p>
<divclass="sourceCode cell-code" id="cb2"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb2-1"><ahref="#cb2-1" aria-hidden="true" tabindex="-1"></a><spanclass="co"># a) # </span><spanclass="al">TODO</span><spanclass="co">: Create a class called 'Book' with the following specifications:</span></span>
342
342
<spanid="cb2-2"><ahref="#cb2-2" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - It should have attributes for 'title', 'author', and 'pages'</span></span>
343
343
<spanid="cb2-3"><ahref="#cb2-3" aria-hidden="true" tabindex="-1"></a><spanclass="co"># - Include a method called 'display_info' that prints all the book's information</span></span>
@@ -363,10 +363,11 @@ <h1>Different classes for different tasks</h1>
<p>Remember the number guessing game from the previous tutorial? Well, it’s time to rebuild it. This time, we will use functions to organize the code and make it more reusable. You will learn how to use functions to modularize your code and make it more readable.</p>
<divclass="sourceCode cell-code" id="cb3"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb3-1"><ahref="#cb3-1" aria-hidden="true" tabindex="-1"></a><spanclass="co"># </span><spanclass="al">TODO</span><spanclass="co">: Start by looking at your code from the previous tutorial and think about the different steps that you implemented. </span></span>
368
-
<spanid="cb3-2"><ahref="#cb3-2" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Then, try to modularize each step of the game into a separate function. Name the functions appropriately, to make clear what they do.</span></span>
369
-
<spanid="cb3-3"><ahref="#cb3-3" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
368
+
<spanid="cb3-2"><ahref="#cb3-2" aria-hidden="true" tabindex="-1"></a><spanclass="co"># a) Try to modularize each step of the game into a separate function. Name the functions appropriately, to make clear what they do.</span></span>
369
+
<spanid="cb3-3"><ahref="#cb3-3" aria-hidden="true" tabindex="-1"></a><spanclass="co"># b) Enhance the game by adding difficulty levels as described in the bonus task from the previous tutorial. The difference here is that the difficulty level should be given as a parameter to a function. If no difficulty level is given, i.e. the game master just enters an empty input, the function should use a default value of "Medium".</span></span>
370
+
<spanid="cb3-4"><ahref="#cb3-4" aria-hidden="true" tabindex="-1"></a><spanclass="co"># Your code here</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
0 commit comments