diff --git a/core/chapters/c12_dictionaries.py b/core/chapters/c12_dictionaries.py index 6b70b08c..0249df55 100644 --- a/core/chapters/c12_dictionaries.py +++ b/core/chapters/c12_dictionaries.py @@ -2,17 +2,14 @@ import ast import random from collections import Counter -from typing import List, Dict +from copy import deepcopy +from typing import Dict, List from core import translation as t from core.exercises import assert_equal from core.exercises import generate_string, generate_dict -from core.text import ( - ExerciseStep, - Page, - Step, - VerbatimStep, -) +from core.text import Page, VerbatimStep, ExerciseStep, Step +from core.utils import returns_stdout, wrap_solution # Similar to word_must_be_hello @@ -668,3 +665,627 @@ def print_words(words): final_text = """ Congratulations! You've reached the end of the course so far. More is on the way! """ + # TODO + + +class CreatingKeyValuePairs(Page): + title = "Creating Key-Value Pairs" + + class list_append_reminder(VerbatimStep): + """ + Now we'll learn how to add key-value pairs to a dictionary, + e.g. so that we can keep track of what the customer is buying. + Before looking at dictionaries, let's remind ourselves how to add items to a list. Run this program: + + __copyable__ + __program_indented__ + """ + + def program(self): + cart = [] + cart.append('dog') + cart.append('box') + print(cart) + + class list_assign_reminder(VerbatimStep): + """ + Pretty simple. We can also change the value at an index, replacing it with a different one: + + __copyable__ + __program_indented__ + """ + predicted_output_choices = [ + "['dog', 'box']", + "['box', 'cat']", + "['dog', 'cat']", + ] + + def program(self): + cart = ['dog', 'cat'] + cart[1] = 'box' + print(cart) + + class list_assign_invalid(VerbatimStep): + """ + What if we used that idea to create our list in the first place? + We know we want a list where `cart[0]` is `'dog'` and `cart[1]` is `'box'`, so let's just say that: + + __copyable__ + __program_indented__ + """ + correct_output = "Error" # This program raises an IndexError + + def program(self): + cart = [] + cart[0] = 'dog' + cart[1] = 'box' + print(cart) + + class dict_assignment_valid(VerbatimStep): + """ + Sorry, that's not allowed. For lists, subscript assignment only works for existing valid indices. + But that's not true for dictionaries! Try this: + + __copyable__ + __program_indented__ + + Note that `{}` means an empty dictionary, i.e. a dictionary with no key-value pairs. + This is similar to `[]` meaning an empty list or `""` meaning an empty string. + """ + predicted_output_choices = [ + "{}", + "{'dog': 5000000}", + "{'box': 2}", + "{'dog': 5000000, 'box': 2}", + "{'box': 2, 'dog': 5000000}", # Order might vary, though CPython 3.7+ preserves insertion order + ] + + def program(self): + quantities = {} + quantities['dog'] = 5000000 + quantities['box'] = 2 + print(quantities) + + class buy_quantity_exercise(ExerciseStep): + """ + That's exactly what we need. When the customer says they want 5 million boxes, + we can just put that information directly into our dictionary. So as an exercise, let's make a generic version of that. + Write a function `buy_quantity(quantities)` which calls `input()` twice to get an item name and quantity from the user + and assigns them in the `quantities` dictionary. Here's some starting code: + + __copyable__ + def buy_quantity(quantities): + print('Item:') + item = input() + print('Quantity:') + ... + + def test(): + quantities = {} + buy_quantity(quantities) + print(quantities) + buy_quantity(quantities) + print(quantities) + + test() + + and an example of how a session should look: + + Item: + dog + Quantity: + 5000000 + {'dog': 5000000} + Item: + box + Quantity: + 2 + {'dog': 5000000, 'box': 2} + + Note that `buy_quantity` should *modify* the dictionary that's passed in, and doesn't need to return anything. + You can assume that the user will enter a valid integer for the quantity. + You can also assume that the user won't enter an item that's already in `quantities`. + """ + requirements = "Your function should modify the `quantities` argument. It doesn't need to `return` or `print` anything." + no_returns_stdout = True + + hints = """ + The function needs to get two inputs from the user: the item name and the quantity. + The item name is already stored in the `item` variable. You need to get the quantity similarly. + Remember that `input()` returns a string. The quantity needs to be stored as a number (`int`). + How do you convert a string to an integer? + Once you have the `item` (string) and the quantity (integer), you need to add them to the `quantities` dictionary. + Use the dictionary assignment syntax you just learned: `dictionary[key] = value`. + What should be the key and what should be the value in this case? + """ + + def solution(self): + def buy_quantity(quantities: Dict[str, int]): + print('Item:') + item = input() + print('Quantity:') + quantity_str = input() + quantities[item] = int(quantity_str) + return buy_quantity + + @classmethod + def wrap_solution(cls, func): + @returns_stdout + @wrap_solution(func) + def wrapper(**kwargs): + quantities_name = t.get_code_bit("quantities") + quantities = kwargs[quantities_name] = deepcopy(kwargs[quantities_name]) + + func(**kwargs) + print(quantities) + return wrapper + + @classmethod + def generate_inputs(cls): + return { + "stdin_input": [generate_string(5), str(random.randint(1, 10))], + "quantities": generate_dict(str, int), + } + + tests = [ + ( + dict( + quantities={}, + stdin_input=[ + "dog", "5000000", + ] + ), + """\ +Item: + +Quantity: + +{'dog': 5000000} + """ + ), + ( + dict( + quantities={'dog': 5000000}, + stdin_input=[ + "box", "2", + ] + ), + """\ +Item: + +Quantity: + +{'dog': 5000000, 'box': 2} + """ + ), + ] + + class total_cost_per_item_exercise(ExerciseStep): + """ + Well done! + + Next exercise: earlier we defined a function `total_cost(quantities, prices)` which returned a single number + with a grand total of all the items in the cart. Now let's make a function `total_cost_per_item(quantities, prices)` + which returns a new dictionary with the total cost for each item: + + __copyable__ + def total_cost_per_item(quantities, prices): + totals = {} + for item in quantities: + ... = quantities[item] * prices[item] + return totals + + assert_equal( + total_cost_per_item({'apple': 2}, {'apple': 3, 'box': 5}), + {'apple': 6}, + ) + + assert_equal( + total_cost_per_item({'dog': 5000000, 'box': 2}, {'dog': 100, 'box': 5}), + {'dog': 500000000, 'box': 10}, + ) + """ + hints = """ + You need to iterate through the items in the `quantities` dictionary. + For each `item`, calculate the total cost for that item (quantity * price). + Store this calculated cost in the `totals` dictionary. + The key for the `totals` dictionary should be the `item` name. + Use the dictionary assignment syntax: `totals[item] = calculated_cost`. + Make sure this assignment happens *inside* the loop. + The function should return the `totals` dictionary after the loop finishes. + """ + + def solution(self): + def total_cost_per_item(quantities: Dict[str, int], prices: Dict[str, int]): + totals = {} + for item in quantities: + totals[item] = quantities[item] * prices[item] + return totals + return total_cost_per_item + + tests = [ + (({'apple': 2}, {'apple': 3, 'box': 5}), {'apple': 6}), + (({'dog': 5000000, 'box': 2}, {'dog': 100, 'box': 5}), {'dog': 500000000, 'box': 10}), + (({'pen': 5, 'pencil': 10}, {'pen': 1, 'pencil': 0.5, 'eraser': 2}), {'pen': 5, 'pencil': 5.0}), + (({}, {'apple': 1}), {}), + ] + + @classmethod + def generate_inputs(cls): + prices = generate_dict(str, int) + quantities = {k: random.randint(1, 10) for k in prices if random.choice([True, False])} + return {"quantities": quantities, "prices": prices} + + class make_english_to_german_exercise(ExerciseStep): + """ + Perfect! This is like having a nice receipt full of useful information. + + Let's come back to the example of using dictionaries for translation. Suppose we have one dictionary + for translating from English to French, and another for translating from French to German. + Let's use that to create a dictionary that translates from English to German: + + __copyable__ + def make_english_to_german(english_to_french, french_to_german): + ... + + assert_equal( + make_english_to_german( + {'apple': 'pomme', 'box': 'boite'}, + {'pomme': 'apfel', 'boite': 'kasten'}, + ), + {'apple': 'apfel', 'box': 'kasten'}, + ) + """ + parsons_solution = True + + hints = """ + You need to create a new empty dictionary, let's call it `english_to_german`. + Iterate through the keys (English words) of the `english_to_french` dictionary. + Inside the loop, for each `english` word: + 1. Find the corresponding French word using `english_to_french`. + 2. Use that French word as a key to find the German word in `french_to_german`. + 3. Add the `english` word as a key and the `german` word as the value to your new `english_to_german` dictionary. + After the loop, return the `english_to_german` dictionary. + """ + + def solution(self): + def make_english_to_german(english_to_french: Dict[str, str], french_to_german: Dict[str, str]): + english_to_german = {} + for english in english_to_french: + french = english_to_french[english] + german = french_to_german[french] + english_to_german[english] = german + return english_to_german + return make_english_to_german + + tests = [ + (({'apple': 'pomme', 'box': 'boite'}, {'pomme': 'apfel', 'boite': 'kasten'}), + {'apple': 'apfel', 'box': 'kasten'}), + (({'one': 'un', 'two': 'deux', 'three': 'trois'}, {'un': 'eins', 'deux': 'zwei', 'trois': 'drei'}), + {'one': 'eins', 'two': 'zwei', 'three': 'drei'}), + (({}, {}), {}), + ] + + @classmethod + def generate_inputs(cls): + english_to_french = generate_dict(str, str) + french_to_german = {v: generate_string() for v in english_to_french.values()} + return {"english_to_french": english_to_french, "french_to_german": french_to_german} + + class swap_keys_values_exercise(ExerciseStep): + """ + Great job! + + Of course, language isn't so simple, and there are many ways that using a dictionary like this could go wrong. + So...let's do something even worse! Write a function which takes a dictionary and swaps the keys and values, + so `a: b` becomes `b: a`. + + __copyable__ + def swap_keys_values(d): + ... + + assert_equal( + swap_keys_values({'apple': 'pomme', 'box': 'boite'}), + {'pomme': 'apple', 'boite': 'box'}, + ) + """ + hints = """ + Create a new empty dictionary to store the result. + Iterate through the keys of the input dictionary `d`. + Inside the loop, for each `key`: + 1. Get the corresponding `value` from `d`. + 2. Add an entry to the new dictionary where the *key* is the original `value` and the *value* is the original `key`. + Return the new dictionary after the loop. + """ + + def solution(self): + def swap_keys_values(d: Dict[str, str]): + new_dict = {} + for key in d: + value = d[key] + new_dict[value] = key + return new_dict + return swap_keys_values + + tests = [ + (({'apple': 'pomme', 'box': 'boite'},), {'pomme': 'apple', 'boite': 'box'}), + (({'a': 1, 'b': 2},), {1: 'a', 2: 'b'}), + (({10: 'x', 20: 'y'},), {'x': 10, 'y': 20}), + (({},), {}), + ] + + final_text = """ +Magnificent! + +Jokes aside, it's important to remember how exactly this can go wrong. Just like multiple items in the store +can have the same price, multiple words in English can have the same translation in French. If the original dictionary +has duplicate *values*, what happens when you try to swap keys and values? Since dictionary keys must be unique, +some data will be lost. + +But there are many situations where you can be sure that the values in a dictionary *are* unique and that this +'inversion' makes sense. For example, we saw this code [earlier in the chapter](#UsingDictionaries): + + __copyable__ + __no_auto_translate__ + def substitute(string, d): + result = "" + for letter in string: + result += d[letter] + return result + + plaintext = 'helloworld' + encrypted = 'qpeefifmez' + letters = {'h': 'q', 'e': 'p', 'l': 'e', 'o': 'f', 'w': 'i', 'r': 'm', 'd': 'z'} + reverse = {'q': 'h', 'p': 'e', 'e': 'l', 'f': 'o', 'i': 'w', 'm': 'r', 'z': 'd'} + assert_equal(substitute(plaintext, letters), encrypted) + assert_equal(substitute(encrypted, reverse), plaintext) + +Now we can construct the `reverse` dictionary automatically: + + reverse = swap_keys_values(letters) + +For this to work, we just have to make sure that all the values in `letters` are unique. +Otherwise it would be impossible to decrypt messages properly. If both `'h'` and `'j'` got replaced with `'q'` +during encryption, there would be no way to know whether `'qpeef'` means `'hello'` or `'jello'`! +""" + + +class CopyingDictionaries(Page): + title = "Copying Dictionaries" + + class shared_references(VerbatimStep): + """ + Remember how assigning one list variable to another (`list2 = list1`) made both names point to the *same* list? Dictionaries work the same way because they are also *mutable* (can be changed). + + Predict what the following code will print, then run it to see: + + __copyable__ + __program_indented__ + """ + def program(self): + d1 = {'a': 1, 'b': 2} + d2 = d1 + + print("d1 before:", d1) + print("d2 before:", d2) + print("Are they the same object?", d1 is d2) + + d2['c'] = 3 # Modify via d2 + + print("d1 after:", d1) # Is d1 affected? + print("d2 after:", d2) + + predicted_output_choices = [ + # Incorrect prediction (d1 unaffected) + """d1 before: {'a': 1, 'b': 2} +d2 before: {'a': 1, 'b': 2} +Are they the same object? True +d1 after: {'a': 1, 'b': 2} +d2 after: {'a': 1, 'b': 2, 'c': 3}""", + + # Correct prediction + """d1 before: {'a': 1, 'b': 2} +d2 before: {'a': 1, 'b': 2} +Are they the same object? True +d1 after: {'a': 1, 'b': 2, 'c': 3} +d2 after: {'a': 1, 'b': 2, 'c': 3}""", + + # Incorrect prediction (is False) + """d1 before: {'a': 1, 'b': 2} +d2 before: {'a': 1, 'b': 2} +Are they the same object? False +d1 after: {'a': 1, 'b': 2} +d2 after: {'a': 1, 'b': 2, 'c': 3}""", + ] + + class making_copies(VerbatimStep): + """ + Because `d1` and `d2` referred to the exact same dictionary object (`d1 is d2` was `True`), changing it via `d2` also changed what `d1` saw. + + To get a *separate* dictionary with the same contents, use the `.copy()` method. + + Predict how using `.copy()` changes the outcome, then run this code: + + __copyable__ + __program_indented__ + """ + def program(self): + d1 = {'a': 1, 'b': 2} + d2 = d1.copy() # Create a separate copy + + print("d1 before:", d1) + print("d2 before:", d2) + print("Are they the same object?", d1 is d2) + + d2['c'] = 3 # Modify the copy + + print("d1 after:", d1) # Is d1 affected now? + print("d2 after:", d2) + + predicted_output_choices = [ + # Incorrect prediction (is True) + """d1 before: {'a': 1, 'b': 2} +d2 before: {'a': 1, 'b': 2} +Are they the same object? True +d1 after: {'a': 1, 'b': 2, 'c': 3} +d2 after: {'a': 1, 'b': 2, 'c': 3}""", + + # Incorrect prediction (d1 affected) + """d1 before: {'a': 1, 'b': 2} +d2 before: {'a': 1, 'b': 2} +Are they the same object? False +d1 after: {'a': 1, 'b': 2, 'c': 3} +d2 after: {'a': 1, 'b': 2, 'c': 3}""", + + # Correct prediction + """d1 before: {'a': 1, 'b': 2} +d2 before: {'a': 1, 'b': 2} +Are they the same object? False +d1 after: {'a': 1, 'b': 2} +d2 after: {'a': 1, 'b': 2, 'c': 3}""", + ] + + class positive_stock_exercise(ExerciseStep): + """ + Making an exact copy is useful, but often we want a *modified* copy. Let's practice creating a new dictionary based on an old one. + + Write a function `positive_stock(stock)` that takes a dictionary `stock` (mapping item names to integer quantities) and returns a *new* dictionary containing only the items from the original `stock` where the quantity is strictly greater than 0. The original `stock` dictionary should not be changed. + + __copyable__ + def positive_stock(stock): + # Your code here + ... + + assert_equal( + positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), + {'apple': 10, 'pear': 5} + ) + assert_equal( + positive_stock({'pen': 0, 'pencil': 0}), + {} + ) + assert_equal( + positive_stock({'book': 1, 'paper': 5}), + {'book': 1, 'paper': 5} + ) + """ + hints = """ + Start by creating a new empty dictionary, e.g., `result = {}`. + Loop through the keys of the input `stock` dictionary. + Inside the loop, get the `quantity` for the current `item` using `stock[item]`. + Use an `if` statement to check if `quantity > 0`. + If the quantity is positive, add the `item` and its `quantity` to your `result` dictionary using `result[item] = quantity`. + After the loop finishes, return the `result` dictionary. + Make sure you don't modify the original `stock` dictionary passed into the function. Creating a new `result` dictionary ensures this. + """ + + def solution(self): + def positive_stock(stock: Dict[str, int]): + result = {} + for item in stock: + quantity = stock[item] + if quantity > 0: + result[item] = quantity + return result + return positive_stock + + tests = [ + (({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0},), {'apple': 10, 'pear': 5}), + (({'pen': 0, 'pencil': 0},), {}), + (({'book': 1, 'paper': 5},), {'book': 1, 'paper': 5}), + (({},), {}), # Empty input + (({'gadget': -5, 'widget': 3},), {'widget': 3}), # Negative values + ] + + @classmethod + def generate_inputs(cls): + # Generate a dictionary with some zero/negative and positive values + stock = {} + num_items = random.randint(3, 8) + for _ in range(num_items): + item = generate_string(random.randint(3, 6)) + # Ensure some variety in quantities + if random.random() < 0.4: + quantity = 0 + elif random.random() < 0.2: + quantity = random.randint(-5, -1) + else: + quantity = random.randint(1, 20) + stock[item] = quantity + # Ensure at least one positive if dict not empty + if stock and all(q <= 0 for q in stock.values()): + stock[generate_string(4)] = random.randint(1, 10) + return {"stock": stock} + + class add_item_exercise(ExerciseStep): + """ + Let's practice combining copying and modifying. Imagine we want to represent adding one unit of an item to our stock count. + + Write a function `add_item(item, quantities)` that takes an item name (`item`) and a dictionary `quantities`. You can assume the `item` *already exists* as a key in the `quantities` dictionary. + + The function should return a *new* dictionary which is a copy of `quantities`, but with the value associated with `item` increased by 1. The original `quantities` dictionary should not be changed. + + __copyable__ + def add_item(item, quantities): + # Your code here + ... + + stock = {'apple': 5, 'banana': 2} + new_stock = add_item('apple', stock) + assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged + assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value + + new_stock_2 = add_item('banana', new_stock) + assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged + assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented + """ + hints = """ + First, create a *copy* of the input `quantities` dictionary using the `.copy()` method. Store this in a new variable, e.g., `new_quantities`. + Since we assume `item` is already a key, you don't need to check for its existence in this exercise. + Find the current quantity of the `item` in the `new_quantities` copy using `new_quantities[item]`. + Calculate the new quantity by adding 1 to the current quantity. + Update the value for `item` in the `new_quantities` copy with this new quantity using assignment: `new_quantities[item] = ...`. + Return the `new_quantities` dictionary. + """ + + def solution(self): + def add_item(item: str, quantities: Dict[str, int]): + new_quantities = quantities.copy() + new_quantities[item] = new_quantities[item] + 1 + return new_quantities + return add_item + + tests = [ + (('apple', {'apple': 5, 'banana': 2}), {'apple': 6, 'banana': 2}), + (('banana', {'apple': 6, 'banana': 2}), {'apple': 6, 'banana': 3}), + (('pen', {'pen': 1}), {'pen': 2}), + (('a', {'a': 0, 'b': 99}), {'a': 1, 'b': 99}), + ] + + @classmethod + def generate_inputs(cls): + quantities = generate_dict(str, int) + # Ensure the dictionary is not empty + if not quantities: + quantities[generate_string(4)] = random.randint(0, 10) + # Pick an existing item to increment + item = random.choice(list(quantities.keys())) + return {"item": item, "quantities": quantities} + + final_text = """ + Well done! Notice that the line where you increment the value: + + new_quantities[item] = new_quantities[item] + 1 + + can also be written more concisely using the `+=` operator, just like with numbers: + + new_quantities[item] += 1 + + This does the same thing: it reads the current value, adds 1, and assigns the result back. + """ + + final_text = """ + Great! You now know why copying dictionaries is important (because they are mutable) and how to do it using `.copy()`. You've also practiced creating modified copies, which is a common and safe way to work with data without accidentally changing things elsewhere in your program. + + Next, we'll see how to check if a key exists *before* trying to use it, to avoid errors. + """ \ No newline at end of file diff --git a/tests/golden_files/en/test_transcript.json b/tests/golden_files/en/test_transcript.json index 4ba82422..99b77e70 100644 --- a/tests/golden_files/en/test_transcript.json +++ b/tests/golden_files/en/test_transcript.json @@ -7228,5 +7228,319 @@ ] }, "step": "nested_dictionaries" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "cart = []", + "cart.append('dog')", + "cart.append('box')", + "print(cart)" + ], + "response": { + "passed": true, + "result": [ + { + "text": "['dog', 'box']\n", + "type": "stdout" + } + ] + }, + "step": "list_append_reminder" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "cart = ['dog', 'cat']", + "cart[1] = 'box'", + "print(cart)" + ], + "response": { + "passed": true, + "prediction": { + "answer": "['dog', 'box']", + "choices": [ + "['dog', 'box']", + "['box', 'cat']", + "['dog', 'cat']", + "Error" + ] + }, + "result": [ + { + "text": "['dog', 'box']\n", + "type": "stdout" + } + ] + }, + "step": "list_assign_reminder" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "cart = []", + "cart[0] = 'dog'", + "cart[1] = 'box'", + "print(cart)" + ], + "response": { + "passed": true, + "result": [ + { + "data": [ + { + "didyoumean": [], + "exception": { + "message": "list assignment index out of range", + "type": "IndexError" + }, + "frames": [ + { + "filename": "/my_program.py", + "lineno": 2, + "lines": [ + { + "is_current": true, + "lineno": 2, + "text": "cart[0] = 'dog'", + "type": "line" + } + ], + "name": "", + "type": "frame", + "variables": [ + { + "name": "cart\n", + "value": "[]\n" + } + ] + } + ], + "friendly": "

An IndexError occurs when you try to get an item from a list,\na tuple, or a similar object (sequence), and use an index which\ndoes not exist; typically, this happens because the index you give\nis greater than the length of the sequence.

\n

You have tried to assign a value to index 0 of cart,\na list which contains no item.

", + "tail": "" + } + ], + "text": [ + "Traceback (most recent call last):", + " File \"/my_program.py\", line 2, in ", + " 1 | cart = []", + "--> 2 | cart[0] = 'dog'", + " ^^^^^^^", + "cart = []", + "", + "IndexError: list assignment index out of range" + ], + "type": "traceback" + } + ] + }, + "step": "list_assign_invalid" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "quantities = {}", + "quantities['dog'] = 5000000", + "quantities['box'] = 2", + "print(quantities)" + ], + "response": { + "passed": true, + "prediction": { + "answer": "{'dog': 5000000, 'box': 2}", + "choices": [ + "{}", + "{'dog': 5000000}", + "{'box': 2}", + "{'dog': 5000000, 'box': 2}", + "{'box': 2, 'dog': 5000000}", + "Error" + ] + }, + "result": [ + { + "text": "{'dog': 5000000, 'box': 2}\n", + "type": "stdout" + } + ] + }, + "step": "dict_assignment_valid" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "def buy_quantity(quantities):", + " print('Item:')", + " item = input()", + " print('Quantity:')", + " quantity_str = input()", + " quantities[item] = int(quantity_str)" + ], + "response": { + "passed": true, + "result": [] + }, + "step": "buy_quantity_exercise" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "def total_cost_per_item(quantities, prices):", + " totals = {}", + " for item in quantities:", + " totals[item] = quantities[item] * prices[item]", + " return totals" + ], + "response": { + "passed": true, + "result": [] + }, + "step": "total_cost_per_item_exercise" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "def make_english_to_german(english_to_french, french_to_german):", + " english_to_german = {}", + " for english in english_to_french:", + " french = english_to_french[english]", + " german = french_to_german[french]", + " english_to_german[english] = german", + " return english_to_german" + ], + "response": { + "passed": true, + "result": [] + }, + "step": "make_english_to_german_exercise" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "def swap_keys_values(d):", + " new_dict = {}", + " for key in d:", + " value = d[key]", + " new_dict[value] = key", + " return new_dict" + ], + "response": { + "passed": true, + "result": [] + }, + "step": "swap_keys_values_exercise" + }, + { + "get_solution": "program", + "page": "Copying Dictionaries", + "program": [ + "d1 = {'a': 1, 'b': 2}", + "d2 = d1", + "", + "print(\"d1 before:\", d1)", + "print(\"d2 before:\", d2)", + "print(\"Are they the same object?\", d1 is d2)", + "", + "d2['c'] = 3 # Modify via d2", + "", + "print(\"d1 after:\", d1) # Is d1 affected?", + "print(\"d2 after:\", d2)" + ], + "response": { + "passed": true, + "prediction": { + "answer": "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? True\nd1 after: {'a': 1, 'b': 2, 'c': 3}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "choices": [ + "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? True\nd1 after: {'a': 1, 'b': 2}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? True\nd1 after: {'a': 1, 'b': 2, 'c': 3}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? False\nd1 after: {'a': 1, 'b': 2}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "Error" + ] + }, + "result": [ + { + "text": "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? True\nd1 after: {'a': 1, 'b': 2, 'c': 3}\nd2 after: {'a': 1, 'b': 2, 'c': 3}\n", + "type": "stdout" + } + ] + }, + "step": "shared_references" + }, + { + "get_solution": "program", + "page": "Copying Dictionaries", + "program": [ + "d1 = {'a': 1, 'b': 2}", + "d2 = d1.copy() # Create a separate copy", + "", + "print(\"d1 before:\", d1)", + "print(\"d2 before:\", d2)", + "print(\"Are they the same object?\", d1 is d2)", + "", + "d2['c'] = 3 # Modify the copy", + "", + "print(\"d1 after:\", d1) # Is d1 affected now?", + "print(\"d2 after:\", d2)" + ], + "response": { + "passed": true, + "prediction": { + "answer": "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? False\nd1 after: {'a': 1, 'b': 2}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "choices": [ + "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? True\nd1 after: {'a': 1, 'b': 2, 'c': 3}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? False\nd1 after: {'a': 1, 'b': 2, 'c': 3}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? False\nd1 after: {'a': 1, 'b': 2}\nd2 after: {'a': 1, 'b': 2, 'c': 3}", + "Error" + ] + }, + "result": [ + { + "text": "d1 before: {'a': 1, 'b': 2}\nd2 before: {'a': 1, 'b': 2}\nAre they the same object? False\nd1 after: {'a': 1, 'b': 2}\nd2 after: {'a': 1, 'b': 2, 'c': 3}\n", + "type": "stdout" + } + ] + }, + "step": "making_copies" + }, + { + "get_solution": "program", + "page": "Copying Dictionaries", + "program": [ + "def positive_stock(stock):", + " result = {}", + " for item in stock:", + " quantity = stock[item]", + " if quantity > 0:", + " result[item] = quantity", + " return result" + ], + "response": { + "passed": true, + "result": [] + }, + "step": "positive_stock_exercise" + }, + { + "get_solution": "program", + "page": "Copying Dictionaries", + "program": [ + "def add_item(item, quantities):", + " new_quantities = quantities.copy()", + " new_quantities[item] = new_quantities[item] + 1", + " return new_quantities" + ], + "response": { + "passed": true, + "result": [] + }, + "step": "add_item_exercise" } ] \ No newline at end of file diff --git a/translations/english.po b/translations/english.po index ea379858..979f5305 100644 --- a/translations/english.po +++ b/translations/english.po @@ -536,6 +536,38 @@ msgstr "\"Alice's Diner\"" msgid "code_bits.\"Amazing! Are you psychic?\"" msgstr "\"Amazing! Are you psychic?\"" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1.copy() # Create a separate copy +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify the copy +#. +#. print("d1 after:", d1) # Is d1 affected now? +#. print("d2 after:", d2) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1 +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify via d2 +#. +#. print("d1 after:", d1) # Is d1 affected? +#. print("d2 after:", d2) +msgid "code_bits.\"Are they the same object?\"" +msgstr "\"Are they the same object?\"" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DefiningFunctions.steps.change_function_name #. #. def say_hello(name): @@ -1372,6 +1404,134 @@ msgstr "\"abc\"" msgid "code_bits.\"cat.jpg\"" msgstr "\"cat.jpg\"" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1.copy() # Create a separate copy +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify the copy +#. +#. print("d1 after:", d1) # Is d1 affected now? +#. print("d2 after:", d2) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1 +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify via d2 +#. +#. print("d1 after:", d1) # Is d1 affected? +#. print("d2 after:", d2) +msgid "code_bits.\"d1 after:\"" +msgstr "\"d1 after:\"" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1.copy() # Create a separate copy +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify the copy +#. +#. print("d1 after:", d1) # Is d1 affected now? +#. print("d2 after:", d2) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1 +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify via d2 +#. +#. print("d1 after:", d1) # Is d1 affected? +#. print("d2 after:", d2) +msgid "code_bits.\"d1 before:\"" +msgstr "\"d1 before:\"" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1.copy() # Create a separate copy +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify the copy +#. +#. print("d1 after:", d1) # Is d1 affected now? +#. print("d2 after:", d2) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1 +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify via d2 +#. +#. print("d1 after:", d1) # Is d1 affected? +#. print("d2 after:", d2) +msgid "code_bits.\"d2 after:\"" +msgstr "\"d2 after:\"" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1.copy() # Create a separate copy +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify the copy +#. +#. print("d1 after:", d1) # Is d1 affected now? +#. print("d2 after:", d2) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1 +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify via d2 +#. +#. print("d1 after:", d1) # Is d1 affected? +#. print("d2 after:", d2) +msgid "code_bits.\"d2 before:\"" +msgstr "\"d2 before:\"" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingNestedLists.steps.double_subscripting #. #. strings = ["abc", "def", "ghi"] @@ -2512,6 +2672,36 @@ msgstr "'Hello there'" msgid "code_bits.'Hello'" msgstr "'Hello'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. quantity_str = input() +#. quantities[item] = int(quantity_str) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities) +#. print(quantities) +#. buy_quantity(quantities) +#. print(quantities) +#. +#. test() +msgid "code_bits.'Item:'" +msgstr "'Item:'" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IfAndElse.steps.first_if_else #. #. condition = True @@ -2572,6 +2762,36 @@ msgstr "'One more exercise, and then you can relax.'" msgid "code_bits.'Python'" msgstr "'Python'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. quantity_str = input() +#. quantities[item] = int(quantity_str) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities) +#. print(quantities) +#. buy_quantity(quantities) +#. print(quantities) +#. +#. test() +msgid "code_bits.'Quantity:'" +msgstr "'Quantity:'" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.Types.steps.fixing_type_errors_with_conversion #. #. number = '1' @@ -3185,6 +3405,21 @@ msgstr "'abcqwe'" msgid "code_bits.'aeiou'" msgstr "'aeiou'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_german.text #. #. def print_words(french, german): @@ -3221,6 +3456,71 @@ msgstr "'aeiou'" msgid "code_bits.'apfel'" msgstr "'apfel'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.text +#. +#. def swap_keys_values(d): +#. ... +#. +#. assert_equal( +#. swap_keys_values({'apple': 'pomme', 'box': 'boite'}), +#. {'pomme': 'apple', 'boite': 'box'}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -3400,6 +3700,44 @@ msgstr "'apple'" msgid "code_bits.'are'" msgstr "'are'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.'banana'" +msgstr "'banana'" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.TheEqualityOperator.steps.introducing_equality #. #. print(1 + 2 == 3) @@ -3408,6 +3746,33 @@ msgstr "'are'" msgid "code_bits.'bc'" msgstr "'bc'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.text +#. +#. def swap_keys_values(d): +#. ... +#. +#. assert_equal( +#. swap_keys_values({'apple': 'pomme', 'box': 'boite'}), +#. {'pomme': 'apple', 'boite': 'box'}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_french.text #. #. def print_words(french): @@ -3459,6 +3824,89 @@ msgstr "'bc'" msgid "code_bits.'boite'" msgstr "'boite'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.'book'" +msgstr "'book'" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +#. +#. quantities = {} +#. quantities['dog'] = 5000000 +#. quantities['box'] = 2 +#. print(quantities) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +#. +#. cart = [] +#. cart.append('dog') +#. cart.append('box') +#. print(cart) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +#. +#. cart = [] +#. cart[0] = 'dog' +#. cart[1] = 'box' +#. print(cart) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder +#. +#. cart = ['dog', 'cat'] +#. cart[1] = 'box' +#. print(cart) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.text +#. +#. def swap_keys_values(d): +#. ... +#. +#. assert_equal( +#. swap_keys_values({'apple': 'pomme', 'box': 'boite'}), +#. {'pomme': 'apple', 'boite': 'box'}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -3582,6 +4030,14 @@ msgstr "'boite'" msgid "code_bits.'box'" msgstr "'box'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder +#. +#. cart = ['dog', 'cat'] +#. cart[1] = 'box' +#. print(cart) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -3723,6 +4179,41 @@ msgstr "'de'" msgid "code_bits.'def'" msgstr "'def'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +#. +#. quantities = {} +#. quantities['dog'] = 5000000 +#. quantities['box'] = 2 +#. print(quantities) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +#. +#. cart = [] +#. cart.append('dog') +#. cart.append('box') +#. print(cart) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +#. +#. cart = [] +#. cart[0] = 'dog' +#. cart[1] = 'box' +#. print(cart) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder +#. +#. cart = ['dog', 'cat'] +#. cart[1] = 'box' +#. print(cart) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -4102,6 +4593,21 @@ msgstr "'is'" msgid "code_bits.'jklmn'" msgstr "'jklmn'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_german.text #. #. def print_words(french, german): @@ -4220,23 +4726,155 @@ msgstr "'list'" msgid "code_bits.'on'" msgstr "'on'" -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_french.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text #. -#. def print_words(french): +#. def positive_stock(stock): +#. # Your code here #. ... #. -#. print_words({'apple': 'pomme', 'box': 'boite'}) -#. -#. ------ -#. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_german.text +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.'orange'" +msgstr "'orange'" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text #. -#. def print_words(french, german): +#. def positive_stock(stock): +#. # Your code here #. ... #. -#. print_words( -#. {'apple': 'pomme', 'box': 'boite'}, -#. {'apple': 'apfel', 'box': 'kasten'}, +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.'paper'" +msgstr "'paper'" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.'pear'" +msgstr "'pear'" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.'pen'" +msgstr "'pen'" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.'pencil'" +msgstr "'pencil'" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.text +#. +#. def swap_keys_values(d): +#. ... +#. +#. assert_equal( +#. swap_keys_values({'apple': 'pomme', 'box': 'boite'}), +#. {'pomme': 'apple', 'boite': 'box'}, +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_french.text +#. +#. def print_words(french): +#. ... +#. +#. print_words({'apple': 'pomme', 'box': 'boite'}) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_german.text +#. +#. def print_words(french, german): +#. ... +#. +#. print_words( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'apple': 'apfel', 'box': 'kasten'}, #. ) #. #. ------ @@ -4374,6 +5012,42 @@ msgstr "'you'" msgid "code_bits.Hello" msgstr "Hello" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder.text +#. +#. __program_indented__ +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.keys_are_iterable.text #. #. __program_indented__ @@ -4660,6 +5334,32 @@ msgstr "__program_indented__" msgid "code_bits.actual" msgstr "actual" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +#. +#. def add_item(item, quantities): +#. new_quantities = quantities.copy() +#. new_quantities[item] = new_quantities[item] + 1 +#. return new_quantities +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +msgid "code_bits.add_item" +msgstr "add_item" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingFstrings.steps.basic_f_string_exercise #. #. name = "Alice" @@ -4857,6 +5557,71 @@ msgstr "all_numbers" #. #. ------ #. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.text +#. +#. def swap_keys_values(d): +#. ... +#. +#. assert_equal( +#. swap_keys_values({'apple': 'pomme', 'box': 'boite'}), +#. {'pomme': 'apple', 'boite': 'box'}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -6422,6 +7187,36 @@ msgstr "board" msgid "code_bits.board_size" msgstr "board_size" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. quantity_str = input() +#. quantities[item] = int(quantity_str) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities) +#. print(quantities) +#. buy_quantity(quantities) +#. print(quantities) +#. +#. test() +msgid "code_bits.buy_quantity" +msgstr "buy_quantity" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingNestedLoops.steps.crack_password_exercise #. #. letters = 'AB' @@ -6466,6 +7261,32 @@ msgstr "c3" msgid "code_bits.c4" msgstr "c4" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +#. +#. cart = [] +#. cart.append('dog') +#. cart.append('box') +#. print(cart) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +#. +#. cart = [] +#. cart[0] = 'dog' +#. cart[1] = 'box' +#. print(cart) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder +#. +#. cart = ['dog', 'cat'] +#. cart[1] = 'box' +#. print(cart) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.UsingDictionaries.steps.shopping_cart1 #. #. def total_cost(cart, prices): @@ -7825,6 +8646,70 @@ msgstr "consonants" msgid "code_bits.cube" msgstr "cube" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1.copy() # Create a separate copy +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify the copy +#. +#. print("d1 after:", d1) # Is d1 affected now? +#. print("d2 after:", d2) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1 +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify via d2 +#. +#. print("d1 after:", d1) # Is d1 affected? +#. print("d2 after:", d2) +msgid "code_bits.d1" +msgstr "d1" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1.copy() # Create a separate copy +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify the copy +#. +#. print("d1 after:", d1) # Is d1 affected now? +#. print("d2 after:", d2) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +#. +#. d1 = {'a': 1, 'b': 2} +#. d2 = d1 +#. +#. print("d1 before:", d1) +#. print("d2 before:", d2) +#. print("Are they the same object?", d1 is d2) +#. +#. d2['c'] = 3 # Modify via d2 +#. +#. print("d1 after:", d1) # Is d1 affected? +#. print("d2 after:", d2) +msgid "code_bits.d2" +msgstr "d2" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.CombiningAndAndOr.steps.final_text.text #. #. diagonal1 = all_equal([board[0][0], board[1][1], board[2][2]]) @@ -8612,6 +9497,57 @@ msgstr "doubles" msgid "code_bits.element" msgstr "element" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. english_to_german = {} +#. for english in english_to_french: +#. french = english_to_french[english] +#. german = french_to_german[french] +#. english_to_german[english] = german +#. return english_to_german +msgid "code_bits.english" +msgstr "english" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. english_to_german = {} +#. for english in english_to_french: +#. french = english_to_french[english] +#. german = french_to_german[french] +#. english_to_german[english] = german +#. return english_to_german +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +msgid "code_bits.english_to_french" +msgstr "english_to_french" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. english_to_german = {} +#. for english in english_to_french: +#. french = english_to_french[english] +#. german = french_to_german[french] +#. english_to_german[english] = german +#. return english_to_german +msgid "code_bits.english_to_german" +msgstr "english_to_german" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingTicTacToe.steps.intro_row_winner #. #. def row_winner(board): @@ -10302,6 +11238,18 @@ msgstr "format_board" msgid "code_bits.found" msgstr "found" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. english_to_german = {} +#. for english in english_to_french: +#. french = english_to_french[english] +#. german = french_to_german[french] +#. english_to_german[english] = german +#. return english_to_german +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_french #. #. def print_words(french): @@ -10374,6 +11322,33 @@ msgstr "found" msgid "code_bits.french" msgstr "french" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. english_to_german = {} +#. for english in english_to_french: +#. french = english_to_french[english] +#. german = french_to_german[french] +#. english_to_german[english] = german +#. return english_to_german +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +msgid "code_bits.french_to_german" +msgstr "french_to_german" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingFstrings.steps.introduce_f_strings #. #. name = "Alice" @@ -10412,6 +11387,18 @@ msgstr "friend" msgid "code_bits.game_board" msgstr "game_board" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. english_to_german = {} +#. for english in english_to_french: +#. french = english_to_french[english] +#. german = french_to_german[french] +#. english_to_german[english] = german +#. return english_to_german +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.english_to_german #. #. def print_words(french, german): @@ -10863,35 +11850,113 @@ msgstr "is_friend" msgid "code_bits.is_valid_percentage" msgstr "is_valid_percentage" -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise #. -#. def total_cost(quantities, prices): -#. result = 0 -#. for item in quantities: -#. price = prices[item] -#. quantity = quantities[item] -#. result += price * quantity -#. return result +#. def add_item(item, quantities): +#. new_quantities = quantities.copy() +#. new_quantities[item] = new_quantities[item] + 1 +#. return new_quantities #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text #. -#. def total_cost(quantities, prices): -#. result = 0 -#. for item in ...: -#. price = prices[item] -#. quantity = quantities[item] -#. result += price * quantity -#. return result +#. def add_item(item, quantities): +#. # Your code here +#. ... #. -#. assert_equal( -#. total_cost( -#. {'dog': 5000000, 'box': 2}, -#. {'apple': 2, 'box': 5, 'cat': 100, 'dog': 100}, -#. ), -#. 500000010, -#. ) +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +#. +#. def positive_stock(stock): +#. result = {} +#. for item in stock: +#. quantity = stock[item] +#. if quantity > 0: +#. result[item] = quantity +#. return result +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. quantity_str = input() +#. quantities[item] = int(quantity_str) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities) +#. print(quantities) +#. buy_quantity(quantities) +#. print(quantities) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +#. +#. def total_cost_per_item(quantities, prices): +#. totals = {} +#. for item in quantities: +#. totals[item] = quantities[item] * prices[item] +#. return totals +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart +#. +#. def total_cost(quantities, prices): +#. result = 0 +#. for item in quantities: +#. price = prices[item] +#. quantity = quantities[item] +#. result += price * quantity +#. return result +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text +#. +#. def total_cost(quantities, prices): +#. result = 0 +#. for item in ...: +#. price = prices[item] +#. quantity = quantities[item] +#. result += price * quantity +#. return result +#. +#. assert_equal( +#. total_cost( +#. {'dog': 5000000, 'box': 2}, +#. {'apple': 2, 'box': 5, 'cat': 100, 'dog': 100}, +#. ), +#. 500000010, +#. ) #. #. ------ #. @@ -11076,6 +12141,17 @@ msgstr "joined_row" msgid "code_bits.joined_rows" msgstr "joined_rows" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. value = d[key] +#. new_dict[value] = key +#. return new_dict +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.keys_are_iterable #. #. quantities = {'apple': 1, 'cat': 10} @@ -11335,6 +12411,12 @@ msgstr "lengths" msgid "code_bits.letter" msgstr "letter" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.final_text.text +#. +#. reverse = swap_keys_values(letters) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingNestedLoops.steps.crack_password_exercise #. #. letters = 'AB' @@ -12113,6 +13195,33 @@ msgstr "make_board" msgid "code_bits.make_cube" msgstr "make_cube" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. english_to_german = {} +#. for english in english_to_french: +#. french = english_to_french[english] +#. german = french_to_german[french] +#. english_to_german[english] = german +#. return english_to_german +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text +#. +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +msgid "code_bits.make_english_to_german" +msgstr "make_english_to_german" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingFstrings.steps.introduce_f_strings #. #. name = "Alice" @@ -12680,6 +13789,17 @@ msgstr "middle" msgid "code_bits.name" msgstr "name" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. value = d[key] +#. new_dict[value] = key +#. return new_dict +msgid "code_bits.new_dict" +msgstr "new_dict" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.TheEqualityOperator.steps.if_equals_replacing_characters #. #. name = 'kesha' @@ -12732,6 +13852,15 @@ msgstr "new_numbers" msgid "code_bits.new_nums" msgstr "new_nums" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +#. +#. def add_item(item, quantities): +#. new_quantities = quantities.copy() +#. new_quantities[item] = new_quantities[item] + 1 +#. return new_quantities +msgid "code_bits.new_quantities" +msgstr "new_quantities" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.CombiningCompoundStatements.steps.final_text.text #. #. sentence = 'Hello World' @@ -12862,6 +13991,40 @@ msgstr "new_nums" msgid "code_bits.new_sentence" msgstr "new_sentence" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +msgid "code_bits.new_stock" +msgstr "new_stock" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +msgid "code_bits.new_stock_2" +msgstr "new_stock_2" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.FunctionsAndMethodsForLists.steps.subscript_assignment_predict.text #. #. some_list[index] = new_value @@ -14519,6 +15682,39 @@ msgstr "player2" msgid "code_bits.players" msgstr "players" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +#. +#. def positive_stock(stock): +#. result = {} +#. for item in stock: +#. quantity = stock[item] +#. if quantity > 0: +#. result[item] = quantity +#. return result +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.positive_stock" +msgstr "positive_stock" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.LoopingOverNestedLists.steps.list_contains_word_exercise #. #. strings = [['hello there', 'how are you'], ['goodbye world', 'hello world']] @@ -14640,6 +15836,16 @@ msgstr "present" msgid "code_bits.price" msgstr "price" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +#. +#. def total_cost_per_item(quantities, prices): +#. totals = {} +#. for item in quantities: +#. totals[item] = quantities[item] * prices[item] +#. return totals +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart #. #. def total_cost(quantities, prices): @@ -15268,6 +16474,81 @@ msgstr "printed" msgid "code_bits.quadruple" msgstr "quadruple" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +#. +#. def add_item(item, quantities): +#. new_quantities = quantities.copy() +#. new_quantities[item] = new_quantities[item] + 1 +#. return new_quantities +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. quantity_str = input() +#. quantities[item] = int(quantity_str) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities) +#. print(quantities) +#. buy_quantity(quantities) +#. print(quantities) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +#. +#. quantities = {} +#. quantities['dog'] = 5000000 +#. quantities['box'] = 2 +#. print(quantities) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +#. +#. def total_cost_per_item(quantities, prices): +#. totals = {} +#. for item in quantities: +#. totals[item] = quantities[item] * prices[item] +#. return totals +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart #. #. def total_cost(quantities, prices): @@ -15363,6 +16644,18 @@ msgstr "quadruple" msgid "code_bits.quantities" msgstr "quantities" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +#. +#. def positive_stock(stock): +#. result = {} +#. for item in stock: +#. quantity = stock[item] +#. if quantity > 0: +#. result[item] = quantity +#. return result +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart #. #. def total_cost(quantities, prices): @@ -15428,6 +16721,29 @@ msgstr "quantities" msgid "code_bits.quantity" msgstr "quantity" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. quantity_str = input() +#. quantities[item] = int(quantity_str) +msgid "code_bits.quantity_str" +msgstr "quantity_str" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +#. +#. def positive_stock(stock): +#. result = {} +#. for item in stock: +#. quantity = stock[item] +#. if quantity > 0: +#. result[item] = quantity +#. return result +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart #. #. def total_cost(quantities, prices): @@ -15581,6 +16897,12 @@ msgstr "quantity" msgid "code_bits.result" msgstr "result" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.final_text.text +#. +#. reverse = swap_keys_values(letters) +msgid "code_bits.reverse" +msgstr "reverse" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingNestedLoops.steps.times_table_exercise #. #. for left in range(12): @@ -17115,6 +18437,56 @@ msgstr "some_list" msgid "code_bits.spaces" msgstr "spaces" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise.text +#. +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +#. +#. def positive_stock(stock): +#. result = {} +#. for item in stock: +#. quantity = stock[item] +#. if quantity > 0: +#. result[item] = quantity +#. return result +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise.text +#. +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +msgid "code_bits.stock" +msgstr "stock" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingNestedLists.steps.double_subscripting.text #. #. string = strings[1] @@ -17961,6 +19333,35 @@ msgstr "super_secret_number" msgid "code_bits.surround" msgstr "surround" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.final_text.text +#. +#. reverse = swap_keys_values(letters) +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. value = d[key] +#. new_dict[value] = key +#. return new_dict +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.text +#. +#. def swap_keys_values(d): +#. ... +#. +#. assert_equal( +#. swap_keys_values({'apple': 'pomme', 'box': 'boite'}), +#. {'pomme': 'apple', 'boite': 'box'}, +#. ) +msgid "code_bits.swap_keys_values" +msgstr "swap_keys_values" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.BuildingUpStrings.steps.name_triangle.text #. #. temp = hello @@ -17975,6 +19376,25 @@ msgstr "surround" msgid "code_bits.temp" msgstr "temp" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities) +#. print(quantities) +#. buy_quantity(quantities) +#. print(quantities) +#. +#. test() +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.MakingTheBoard.steps.final_text.text #. #. def make_board(size): @@ -18434,6 +19854,26 @@ msgstr "total" msgid "code_bits.total_cost" msgstr "total_cost" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +#. +#. def total_cost_per_item(quantities, prices): +#. totals = {} +#. for item in quantities: +#. totals[item] = quantities[item] * prices[item] +#. return totals +msgid "code_bits.total_cost_per_item" +msgstr "total_cost_per_item" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +#. +#. def total_cost_per_item(quantities, prices): +#. totals = {} +#. for item in quantities: +#. totals[item] = quantities[item] * prices[item] +#. return totals +msgid "code_bits.totals" +msgstr "totals" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.nested_dictionaries #. #. def print_words(words): @@ -18526,6 +19966,17 @@ msgstr "upper" msgid "code_bits.valid_image" msgstr "valid_image" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. value = d[key] +#. new_dict[value] = key +#. return new_dict +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.FunctionsAndMethodsForLists.steps.index_predict_exercise.text #. #. some_list.index(value) @@ -21483,6 +22934,747 @@ msgstr "" msgid "pages.CombiningCompoundStatements.title" msgstr "Combining Compound Statements" +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +msgid "pages.CopyingDictionaries.steps.add_item_exercise.hints.0.text" +msgstr "" +"First, create a *copy* of the input `quantities` dictionary using the `.copy()` method. Store this in a new variable, " +"e.g., `new_quantities`." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +msgid "pages.CopyingDictionaries.steps.add_item_exercise.hints.1.text" +msgstr "Since we assume `item` is already a key, you don't need to check for its existence in this exercise." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +msgid "pages.CopyingDictionaries.steps.add_item_exercise.hints.2.text" +msgstr "Find the current quantity of the `item` in the `new_quantities` copy using `new_quantities[item]`." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +msgid "pages.CopyingDictionaries.steps.add_item_exercise.hints.3.text" +msgstr "Calculate the new quantity by adding 1 to the current quantity." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +msgid "pages.CopyingDictionaries.steps.add_item_exercise.hints.4.text" +msgstr "" +"Update the value for `item` in the `new_quantities` copy with this new quantity using assignment: " +"`new_quantities[item] = ...`." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.add_item_exercise +msgid "pages.CopyingDictionaries.steps.add_item_exercise.hints.5.text" +msgstr "Return the `new_quantities` dictionary." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. # __code0__: +#. def add_item(item, quantities): +#. # Your code here +#. ... +#. +#. stock = {'apple': 5, 'banana': 2} +#. new_stock = add_item('apple', stock) +#. assert_equal(stock, {'apple': 5, 'banana': 2}) # Original unchanged +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Copy has incremented value +#. +#. new_stock_2 = add_item('banana', new_stock) +#. assert_equal(new_stock, {'apple': 6, 'banana': 2}) # Previous copy unchanged +#. assert_equal(new_stock_2, {'apple': 6, 'banana': 3}) # New copy incremented +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27apple%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27banana%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.add_item +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.assert_equal +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.item +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.new_stock +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.new_stock_2 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.quantities +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.stock +msgid "pages.CopyingDictionaries.steps.add_item_exercise.text" +msgstr "" +"Let's practice combining copying and modifying. Imagine we want to represent adding one unit of an item to our stock count.\n" +"\n" +"Write a function `add_item(item, quantities)` that takes an item name (`item`) and a dictionary `quantities`. You can assume the `item` *already exists* as a key in the `quantities` dictionary.\n" +"\n" +"The function should return a *new* dictionary which is a copy of `quantities`, but with the value associated with `item` increased by 1. The original `quantities` dictionary should not be changed.\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://futurecoder.io/course/#CopyingDictionaries +msgid "pages.CopyingDictionaries.steps.final_text.text" +msgstr "" +"Great! You now know why copying dictionaries is important (because they are mutable) and how to do it using `.copy()`. You've also practiced creating modified copies, which is a common and safe way to work with data without accidentally changing things elsewhere in your program.\n" +"\n" +"Next, we'll see how to check if a key exists *before* trying to use it, to avoid errors." + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +msgid "pages.CopyingDictionaries.steps.making_copies.output_prediction_choices.0" +msgstr "" +"d1 before: {'a': 1, 'b': 2}\n" +"d2 before: {'a': 1, 'b': 2}\n" +"Are they the same object? True\n" +"d1 after: {'a': 1, 'b': 2, 'c': 3}\n" +"d2 after: {'a': 1, 'b': 2, 'c': 3}" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +msgid "pages.CopyingDictionaries.steps.making_copies.output_prediction_choices.1" +msgstr "" +"d1 before: {'a': 1, 'b': 2}\n" +"d2 before: {'a': 1, 'b': 2}\n" +"Are they the same object? False\n" +"d1 after: {'a': 1, 'b': 2, 'c': 3}\n" +"d2 after: {'a': 1, 'b': 2, 'c': 3}" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.making_copies +msgid "pages.CopyingDictionaries.steps.making_copies.output_prediction_choices.2" +msgstr "" +"d1 before: {'a': 1, 'b': 2}\n" +"d2 before: {'a': 1, 'b': 2}\n" +"Are they the same object? False\n" +"d1 after: {'a': 1, 'b': 2}\n" +"d2 after: {'a': 1, 'b': 2, 'c': 3}" + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. # __code0__: +#. __program_indented__ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.__program_indented__ +msgid "pages.CopyingDictionaries.steps.making_copies.text" +msgstr "" +"Because `d1` and `d2` referred to the exact same dictionary object (`d1 is d2` was `True`), changing it via `d2` also changed what `d1` saw.\n" +"\n" +"To get a *separate* dictionary with the same contents, use the `.copy()` method.\n" +"\n" +"Predict how using `.copy()` changes the outcome, then run this code:\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.hints.0.text" +msgstr "Start by creating a new empty dictionary, e.g., `result = {}`." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.hints.1.text" +msgstr "Loop through the keys of the input `stock` dictionary." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.hints.2.text" +msgstr "Inside the loop, get the `quantity` for the current `item` using `stock[item]`." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.hints.3.text" +msgstr "Use an `if` statement to check if `quantity > 0`." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.hints.4.text" +msgstr "" +"If the quantity is positive, add the `item` and its `quantity` to your `result` dictionary using `result[item] = " +"quantity`." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.hints.5.text" +msgstr "After the loop finishes, return the `result` dictionary." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.positive_stock_exercise +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.hints.6.text" +msgstr "" +"Make sure you don't modify the original `stock` dictionary passed into the function. Creating a new `result` " +"dictionary ensures this." + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. # __code0__: +#. def positive_stock(stock): +#. # Your code here +#. ... +#. +#. assert_equal( +#. positive_stock({'apple': 10, 'banana': 0, 'pear': 5, 'orange': 0}), +#. {'apple': 10, 'pear': 5} +#. ) +#. assert_equal( +#. positive_stock({'pen': 0, 'pencil': 0}), +#. {} +#. ) +#. assert_equal( +#. positive_stock({'book': 1, 'paper': 5}), +#. {'book': 1, 'paper': 5} +#. ) +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27apple%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27banana%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27book%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27orange%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27paper%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27pear%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27pen%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27pencil%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.assert_equal +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.positive_stock +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.stock +msgid "pages.CopyingDictionaries.steps.positive_stock_exercise.text" +msgstr "" +"Making an exact copy is useful, but often we want a *modified* copy. Let's practice creating a new dictionary based on an old one.\n" +"\n" +"Write a function `positive_stock(stock)` that takes a dictionary `stock` (mapping item names to integer quantities) and returns a *new* dictionary containing only the items from the original `stock` where the quantity is strictly greater than 0. The original `stock` dictionary should not be changed.\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +msgid "pages.CopyingDictionaries.steps.shared_references.output_prediction_choices.0" +msgstr "" +"d1 before: {'a': 1, 'b': 2}\n" +"d2 before: {'a': 1, 'b': 2}\n" +"Are they the same object? True\n" +"d1 after: {'a': 1, 'b': 2}\n" +"d2 after: {'a': 1, 'b': 2, 'c': 3}" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +msgid "pages.CopyingDictionaries.steps.shared_references.output_prediction_choices.1" +msgstr "" +"d1 before: {'a': 1, 'b': 2}\n" +"d2 before: {'a': 1, 'b': 2}\n" +"Are they the same object? True\n" +"d1 after: {'a': 1, 'b': 2, 'c': 3}\n" +"d2 after: {'a': 1, 'b': 2, 'c': 3}" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CopyingDictionaries.steps.shared_references +msgid "pages.CopyingDictionaries.steps.shared_references.output_prediction_choices.2" +msgstr "" +"d1 before: {'a': 1, 'b': 2}\n" +"d2 before: {'a': 1, 'b': 2}\n" +"Are they the same object? False\n" +"d1 after: {'a': 1, 'b': 2}\n" +"d2 after: {'a': 1, 'b': 2, 'c': 3}" + +#. https://futurecoder.io/course/#CopyingDictionaries +#. +#. # __code0__: +#. __program_indented__ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.__program_indented__ +msgid "pages.CopyingDictionaries.steps.shared_references.text" +msgstr "" +"Remember how assigning one list variable to another (`list2 = list1`) made both names point to the *same* list? Dictionaries work the same way because they are also *mutable* (can be changed).\n" +"\n" +"Predict what the following code will print, then run it to see:\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://futurecoder.io/course/#CopyingDictionaries +msgid "pages.CopyingDictionaries.title" +msgstr "Copying Dictionaries" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.hints.0.text" +msgstr "The function needs to get two inputs from the user: the item name and the quantity." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.hints.1.text" +msgstr "The item name is already stored in the `item` variable. You need to get the quantity similarly." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.hints.2.text" +msgstr "Remember that `input()` returns a string. The quantity needs to be stored as a number (`int`)." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.hints.3.text" +msgstr "How do you convert a string to an integer?" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.hints.4.text" +msgstr "" +"Once you have the `item` (string) and the quantity (integer), you need to add them to the `quantities` dictionary." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.hints.5.text" +msgstr "Use the dictionary assignment syntax you just learned: `dictionary[key] = value`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.hints.6.text" +msgstr "What should be the key and what should be the value in this case?" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.requirements" +msgstr "Your function should modify the `quantities` argument. It doesn't need to `return` or `print` anything." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. def buy_quantity(quantities): +#. print('Item:') +#. item = input() +#. print('Quantity:') +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities) +#. print(quantities) +#. buy_quantity(quantities) +#. print(quantities) +#. +#. test() +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27Item%3A%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27Quantity%3A%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.buy_quantity +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.item +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.quantities +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.test +msgid "pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text" +msgstr "" +"That's exactly what we need. When the customer says they want 5 million boxes,\n" +"we can just put that information directly into our dictionary. So as an exercise, let's make a generic version of that.\n" +"Write a function `buy_quantity(quantities)` which calls `input()` twice to get an item name and quantity from the user\n" +"and assigns them in the `quantities` dictionary. Here's some starting code:\n" +"\n" +" __copyable__\n" +"__code0__\n" +"\n" +"and an example of how a session should look:\n" +"\n" +" Item:\n" +" dog\n" +" Quantity:\n" +" 5000000\n" +" {'dog': 5000000}\n" +" Item:\n" +" box\n" +" Quantity:\n" +" 2\n" +" {'dog': 5000000, 'box': 2}\n" +"\n" +"Note that `buy_quantity` should *modify* the dictionary that's passed in, and doesn't need to return anything.\n" +"You can assume that the user will enter a valid integer for the quantity.\n" +"You can also assume that the user won't enter an item that's already in `quantities`." + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +msgid "pages.CreatingKeyValuePairs.steps.dict_assignment_valid.output_prediction_choices.1" +msgstr "{'dog': 5000000}" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +msgid "pages.CreatingKeyValuePairs.steps.dict_assignment_valid.output_prediction_choices.2" +msgstr "{'box': 2}" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +msgid "pages.CreatingKeyValuePairs.steps.dict_assignment_valid.output_prediction_choices.3" +msgstr "{'dog': 5000000, 'box': 2}" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +msgid "pages.CreatingKeyValuePairs.steps.dict_assignment_valid.output_prediction_choices.4" +msgstr "{'box': 2, 'dog': 5000000}" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. __program_indented__ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.__program_indented__ +msgid "pages.CreatingKeyValuePairs.steps.dict_assignment_valid.text" +msgstr "" +"Sorry, that's not allowed. For lists, subscript assignment only works for existing valid indices.\n" +"But that's not true for dictionaries! Try this:\n" +"\n" +" __copyable__\n" +"__code0__\n" +"\n" +"Note that `{}` means an empty dictionary, i.e. a dictionary with no key-value pairs.\n" +"This is similar to `[]` meaning an empty list or `\"\"` meaning an empty string." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. reverse = swap_keys_values(letters) +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.letters +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.reverse +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.swap_keys_values +msgid "pages.CreatingKeyValuePairs.steps.final_text.text" +msgstr "" +"Magnificent!\n" +"\n" +"Jokes aside, it's important to remember how exactly this can go wrong. Just like multiple items in the store\n" +"can have the same price, multiple words in English can have the same translation in French. If the original dictionary\n" +"has duplicate *values*, what happens when you try to swap keys and values? Since dictionary keys must be unique,\n" +"some data will be lost.\n" +"\n" +"But there are many situations where you can be sure that the values in a dictionary *are* unique and that this\n" +"'inversion' makes sense. For example, we saw this code [earlier in the chapter](#UsingDictionaries):\n" +"\n" +" __copyable__\n" +" __no_auto_translate__\n" +" def substitute(string, d):\n" +" result = \"\"\n" +" for letter in string:\n" +" result += d[letter]\n" +" return result\n" +"\n" +" plaintext = 'helloworld'\n" +" encrypted = 'qpeefifmez'\n" +" letters = {'h': 'q', 'e': 'p', 'l': 'e', 'o': 'f', 'w': 'i', 'r': 'm', 'd': 'z'}\n" +" reverse = {'q': 'h', 'p': 'e', 'e': 'l', 'f': 'o', 'i': 'w', 'm': 'r', 'z': 'd'}\n" +" assert_equal(substitute(plaintext, letters), encrypted)\n" +" assert_equal(substitute(encrypted, reverse), plaintext)\n" +"\n" +"Now we can construct the `reverse` dictionary automatically:\n" +"\n" +"__code0__\n" +"\n" +"For this to work, we just have to make sure that all the values in `letters` are unique.\n" +"Otherwise it would be impossible to decrypt messages properly. If both `'h'` and `'j'` got replaced with `'q'`\n" +"during encryption, there would be no way to know whether `'qpeef'` means `'hello'` or `'jello'`!" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. __program_indented__ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.__program_indented__ +msgid "pages.CreatingKeyValuePairs.steps.list_append_reminder.text" +msgstr "" +"Now we'll learn how to add key-value pairs to a dictionary,\n" +"e.g. so that we can keep track of what the customer is buying.\n" +"Before looking at dictionaries, let's remind ourselves how to add items to a list. Run this program:\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. __program_indented__ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.__program_indented__ +msgid "pages.CreatingKeyValuePairs.steps.list_assign_invalid.text" +msgstr "" +"What if we used that idea to create our list in the first place?\n" +"We know we want a list where `cart[0]` is `'dog'` and `cart[1]` is `'box'`, so let's just say that:\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_assign_reminder.output_prediction_choices.0" +msgstr "['dog', 'box']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_assign_reminder.output_prediction_choices.1" +msgstr "['box', 'cat']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_assign_reminder.output_prediction_choices.2" +msgstr "['dog', 'cat']" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. __program_indented__ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.__program_indented__ +msgid "pages.CreatingKeyValuePairs.steps.list_assign_reminder.text" +msgstr "" +"Pretty simple. We can also change the value at an index, replacing it with a different one:\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.hints.0.text" +msgstr "You need to create a new empty dictionary, let's call it `english_to_german`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.hints.1.text" +msgstr "Iterate through the keys (English words) of the `english_to_french` dictionary." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.hints.2.text" +msgstr "Inside the loop, for each `english` word:" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.hints.3.text" +msgstr "1. Find the corresponding French word using `english_to_french`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.hints.4.text" +msgstr "2. Use that French word as a key to find the German word in `french_to_german`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.hints.5.text" +msgstr "" +"3. Add the `english` word as a key and the `german` word as the value to your new `english_to_german` dictionary." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.hints.6.text" +msgstr "After the loop, return the `english_to_german` dictionary." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. def make_english_to_german(english_to_french, french_to_german): +#. ... +#. +#. assert_equal( +#. make_english_to_german( +#. {'apple': 'pomme', 'box': 'boite'}, +#. {'pomme': 'apfel', 'boite': 'kasten'}, +#. ), +#. {'apple': 'apfel', 'box': 'kasten'}, +#. ) +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27apfel%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27apple%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27boite%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27box%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27kasten%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27pomme%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.assert_equal +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.english_to_french +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.french_to_german +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.make_english_to_german +msgid "pages.CreatingKeyValuePairs.steps.make_english_to_german_exercise.text" +msgstr "" +"Perfect! This is like having a nice receipt full of useful information.\n" +"\n" +"Let's come back to the example of using dictionaries for translation. Suppose we have one dictionary\n" +"for translating from English to French, and another for translating from French to German.\n" +"Let's use that to create a dictionary that translates from English to German:\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +msgid "pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.hints.0.text" +msgstr "Create a new empty dictionary to store the result." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +msgid "pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.hints.1.text" +msgstr "Iterate through the keys of the input dictionary `d`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +msgid "pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.hints.2.text" +msgstr "Inside the loop, for each `key`:" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +msgid "pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.hints.3.text" +msgstr "1. Get the corresponding `value` from `d`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +msgid "pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.hints.4.text" +msgstr "" +"2. Add an entry to the new dictionary where the *key* is the original `value` and the *value* is the original `key`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise +msgid "pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.hints.5.text" +msgstr "Return the new dictionary after the loop." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. def swap_keys_values(d): +#. ... +#. +#. assert_equal( +#. swap_keys_values({'apple': 'pomme', 'box': 'boite'}), +#. {'pomme': 'apple', 'boite': 'box'}, +#. ) +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27apple%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27boite%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27box%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27pomme%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.assert_equal +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.swap_keys_values +msgid "pages.CreatingKeyValuePairs.steps.swap_keys_values_exercise.text" +msgstr "" +"Great job!\n" +"\n" +"Of course, language isn't so simple, and there are many ways that using a dictionary like this could go wrong.\n" +"So...let's do something even worse! Write a function which takes a dictionary and swaps the keys and values,\n" +"so `a: b` becomes `b: a`.\n" +"\n" +" __copyable__\n" +"__code0__" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.hints.0.text" +msgstr "You need to iterate through the items in the `quantities` dictionary." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.hints.1.text" +msgstr "For each `item`, calculate the total cost for that item (quantity * price)." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.hints.2.text" +msgstr "Store this calculated cost in the `totals` dictionary." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.hints.3.text" +msgstr "The key for the `totals` dictionary should be the `item` name." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.hints.4.text" +msgstr "Use the dictionary assignment syntax: `totals[item] = calculated_cost`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.hints.5.text" +msgstr "Make sure this assignment happens *inside* the loop." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.hints.6.text" +msgstr "The function should return the `totals` dictionary after the loop finishes." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text" +msgstr "" +"Well done!\n" +"\n" +"Next exercise: earlier we defined a function `total_cost(quantities, prices)` which returned a single number\n" +"with a grand total of all the items in the cart. Now let's make a function `total_cost_per_item(quantities, prices)`\n" +"which returns a new dictionary with the total cost for each item:\n" +"\n" +" __copyable__\n" +" def total_cost_per_item(quantities, prices):\n" +" totals = {}\n" +" for item in quantities:\n" +" ... = quantities[item] * prices[item]\n" +" return totals\n" +"\n" +" assert_equal(\n" +" total_cost_per_item({'apple': 2}, {'apple': 3, 'box': 5}),\n" +" {'apple': 6},\n" +" )\n" +"\n" +" assert_equal(\n" +" total_cost_per_item({'dog': 5000000, 'box': 2}, {'dog': 100, 'box': 5}),\n" +" {'dog': 500000000, 'box': 10},\n" +" )" + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +msgid "pages.CreatingKeyValuePairs.title" +msgstr "Creating Key-Value Pairs" + #. https://futurecoder.io/course/#DefiningFunctions #. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DefiningFunctions.steps.change_function_name diff --git a/translations/locales/en/LC_MESSAGES/futurecoder.mo b/translations/locales/en/LC_MESSAGES/futurecoder.mo index 03910361..14d74342 100644 Binary files a/translations/locales/en/LC_MESSAGES/futurecoder.mo and b/translations/locales/en/LC_MESSAGES/futurecoder.mo differ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy