diff --git a/core/chapters/c12_dictionaries.py b/core/chapters/c12_dictionaries.py index 6b70b08c..124d084b 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,716 @@ 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) + + predicted_output_choices = [ + "[]", + "['dog']", + "['box']", + "['dog', 'box']", + "['box', 'dog']", + "['dog', 'dog']", + "['box', 'box']", + ] + + 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__ + """ + + def program(self): + cart = ['dog', 'cat'] + cart[1] = 'box' + print(cart) + + predicted_output_choices = [ + "['box']", + "['dog', 'cat']", + "['box', 'dog']", + "['box', 'cat']", + "['dog', 'box']", + "['cat', 'box']", + "['box', 'dog', 'cat']", + "['dog', 'box', 'cat']", + "['dog', 'cat', 'box']", + ] + + 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__ + """ + + def program(self): + cart = [] + cart[0] = 'dog' + cart[1] = 'box' + print(cart) + + predicted_output_choices = [ + "[]", + "['dog']", + "['box']", + "['dog', 'box']", + "['box', 'dog']", + "['dog', 'dog']", + "['box', 'box']", + ] + + correct_output = "Error" + + 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: + + __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': 500, 'box': 2}", + "{'dog': 2, 'box': 500}", + "{2: 'dog', 500: 'box'}", + "{500: 'dog', 2: 'box'}", + ] + + def program(self): + quantities = {} + quantities['dog'] = 500 + quantities['box'] = 2 + print(quantities) + + class buy_quantity_exercise(ExerciseStep): + """ + That's exactly what we need. Whether the customer says they want 500 or 5 million dogs, + 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, item, quantity)` which adds a new key-value pair to the `quantities` dictionary. + Here's some starting code: + + __copyable__ + def buy_quantity(quantities, item, quantity): + ... + + def test(): + quantities = {} + buy_quantity(quantities, 'dog', 500) + assert_equal(quantities, {'dog': 500}) + buy_quantity(quantities, 'box', 2) + assert_equal(quantities, {'dog': 500, 'box': 2}) + + test() + + Note that `buy_quantity` should *modify* the dictionary that's passed in, and doesn't need to `return` or `print` anything. + You can assume that `item` isn't already in `quantities`. + """ + requirements = "Your function should modify the `quantities` argument. It doesn't need to `return` or `print` anything." + + hints = """ + The body of `buy_quantity` only needs one simple line of code. + It's similar to some of the lines in the previous step, but with variables instead of hardcoded values. + Be careful with quotes! + `'dog'` is an example of a value for `item`. + What would be an example of a value for `quantity`? + For `'dog'`, it was `500` above. + You're making a generic version of `quantities['dog'] = 500`. + The `quantities` part is fine as is. + Also keep the `[]` and `=`. + Remember that `item` is a variable, `'item'` is a string literal. + Replace `'dog'` with `item`. + Replace `500` with `quantity`. + Don't use `'item'` or `'quantity'`, use the variables `item` and `quantity` directly. + """ + + no_returns_stdout = True # because the solution doesn't return anything, returning stdout would be assumed + + def solution(self): + def buy_quantity(quantities: Dict[str, int], item: str, quantity: int): + quantities[item] = quantity + return buy_quantity + + @classmethod + def wrap_solution(cls, func): + @wrap_solution(func) + def wrapper(**kwargs): + quantities_name = t.get_code_bit("quantities") + quantities = kwargs[quantities_name] = deepcopy(kwargs[quantities_name]) + + func(**kwargs) + return quantities + return wrapper + + @classmethod + def generate_inputs(cls): + result = super().generate_inputs() + result["quantities"].pop(result["item"], None) # ensure item is not already in quantities + return result + + tests = [ + ( + dict( + quantities={}, + item='dog', + quantity=500 + ), + {'dog': 500} + ), + ( + dict( + quantities={'dog': 500}, + item='box', + quantity=2 + ), + {'dog': 500, 'box': 2} + ), + ( + dict( + quantities={'apple': 3, 'banana': 5}, + item='orange', + quantity=10 + ), + {'apple': 3, 'banana': 5, 'orange': 10} + ), + ( + dict( + quantities={}, + item='cat', + quantity=1 + ), + {'cat': 1} + ), + ] + + class buy_quantity_input_test(VerbatimStep): + """ + Well done! Try it out interactively: + + __copyable__ + __program_indented__ + + Note the `int(input())` part, because `input()` returns a string, and `quantity` should be an integer + (a whole number). This'll break if you enter something that isn't a number, but that's OK for now. + """ + + stdin_input = ["apple", "3", "banana", "5", "cat", "2"] + + def program(self): + def buy_quantity(quantities, item, quantity): + quantities[item] = quantity + + def test(): + quantities = {} + for _ in range(3): + print('What would you like to buy?') + item = input() + + print('How many?') + quantity = int(input()) + + buy_quantity(quantities, item, quantity) + + print("OK, here's your cart so far:") + print(quantities) + + test() + + class total_cost_per_item_exercise(ExerciseStep): + """ + Thanks for shopping with us! Let's see how much you just spent on each item. + + 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), + {'dog': 50000, 'box': 10}, + ) + """ + hints = """ + You only need to fill in the `___` part. + But if you want, you could also just put a variable name there, and then add a new line below it. + Look at the tests with `assert_equal`. In the first example, the expected output is `{'apple': 6}`. Why? + Because the customer bought 2 apples, and each apple costs 3, so the total cost is `2 * 3 = 6`. + The `'box': 5` part is ignored because the customer didn't buy any boxes. It just means that the price of a box is 5. + You need to add a new key-value pair to a dictionary. + Identify the dictionary, the key, and the value. + They are all present in the given code already. + The value is the total cost for that item, which is the quantity multiplied by the price. + i.e. `quantities[item] * prices[item]`. + The dictionary is the thing that this function creates, builds, and returns. + i.e. `totals`. + Note how `'apple'` is a key in all three dictionaries in that test. + i.e. the dictionaries `quantities`, `prices`, and `totals`. + """ + + requirements = "Run the program above, but replace the `___` with the correct code." + + 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), {'dog': 50000, '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 dictionary and fill it with key-value pairs depending on the two input dictionaries. + You've seen code that does this before. + Specifically the previous step. The overall structure you want is similar to `total_cost_per_item`. + Start by creating a new empty dictionary. + Return the dictionary at the end. Then fill in the code in between. + You need a `for` loop. + The line `totals[item] = quantities[item] * prices[item]` from the previous step is close to what you need. + You don't need to multiply anything with `*`, the names are different, and there's another difference in logic. + Think about what the keys and values of the new dictionary should be. + The keys should be English words, so they should come from the first dictionary. + The values should be German words, so they should come from the second dictionary. + Specifically the keys of your dictionary should be the keys of the first dictionary. + And the values of your dictionary should be the values of the second dictionary. + What about the values of the first dictionary and the keys of the second dictionary? They're important. + Look at the French words `'pomme'` and `'boite'` in the example test. + The values of the first input dictionary are the keys of the second input 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! Let's use an English-to-French dictionary to create a French-to-English dictionary. + Write a function which takes a dictionary returns a new dictionary where the keys and values are swapped, + 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 = """ + Don't modify the input dictionary `d`. + You need to create a new dictionary and fill it with key-value pairs depending on the input dictionary. + You've done this in the previous exercise. The overall structure you want is similar to `make_english_to_german`. + It's actually even simpler, it just might feel weird. + Start by creating a new empty dictionary. Return the dictionary at the end. Put a `for` loop in between. + Think about what the keys and values of the new dictionary should be. + There's only one possible thing for you to loop over. + Use each key in the input dictionary `d` to get the corresponding value. + """ + + 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}), + (({},), {}), + ] + + class avocado_or_lawyer(VerbatimStep): + """ +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. + +For example, 'avocat' in French can mean either 'avocado' and 'lawyer'. So it's easy to translate +'avocado' from English to French, but it's not so clear how to translate 'avocat' back to English. +Try to guess what the following code will print: + + __copyable__ + __program_indented__ + """ + + def program(self): + def swap_keys_values(d): + new_dict = {} + for key in d: + new_dict[d[key]] = key + return new_dict + + print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) + print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) + + final_text = """ +The result depends on the order of the keys in the original dictionary! +If you're not sure why, try running the code with `snoop` or another debugger. + +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. + """ diff --git a/core/utils.py b/core/utils.py index d6a32817..709b9c2b 100644 --- a/core/utils.py +++ b/core/utils.py @@ -56,7 +56,7 @@ def clean_spaces(string): assert spaces <= {" ", "\n"}, (spaces, string) # In translation, special codes like `__copyable__` often get the wrong indentation. # They must be preceded by 0 or 4 spaces. - if re.search(r"^( {1,3}| {5,})_", string, re.MULTILINE): + if re.search(r"^( {1,3}| {5,})__[a-z]+", string, re.MULTILINE): qa_error("Incorrect indentation of code:\n" + string) return string diff --git a/tests/golden_files/en/test_transcript.json b/tests/golden_files/en/test_transcript.json index 4ba82422..fb3ddaa8 100644 --- a/tests/golden_files/en/test_transcript.json +++ b/tests/golden_files/en/test_transcript.json @@ -7228,5 +7228,371 @@ ] }, "step": "nested_dictionaries" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "cart = []", + "cart.append('dog')", + "cart.append('box')", + "print(cart)" + ], + "response": { + "passed": true, + "prediction": { + "answer": "['dog', 'box']", + "choices": [ + "[]", + "['dog']", + "['box']", + "['dog', 'box']", + "['box', 'dog']", + "['dog', 'dog']", + "['box', 'box']", + "Error" + ] + }, + "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": [ + "['box']", + "['dog', 'cat']", + "['box', 'dog']", + "['box', 'cat']", + "['dog', 'box']", + "['cat', 'box']", + "['box', 'dog', 'cat']", + "['dog', 'box', 'cat']", + "['dog', 'cat', 'box']", + "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, + "prediction": { + "answer": "Error", + "choices": [ + "[]", + "['dog']", + "['box']", + "['dog', 'box']", + "['box', 'dog']", + "['dog', 'dog']", + "['box', 'box']", + "Error" + ] + }, + "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'] = 500", + "quantities['box'] = 2", + "print(quantities)" + ], + "response": { + "passed": true, + "prediction": { + "answer": "{'dog': 500, 'box': 2}", + "choices": [ + "{'dog': 500, 'box': 2}", + "{'dog': 2, 'box': 500}", + "{2: 'dog', 500: 'box'}", + "{500: 'dog', 2: 'box'}", + "Error" + ] + }, + "result": [ + { + "text": "{'dog': 500, 'box': 2}\n", + "type": "stdout" + } + ] + }, + "step": "dict_assignment_valid" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "def buy_quantity(quantities, item, quantity):", + " quantities[item] = quantity" + ], + "response": { + "passed": true, + "result": [] + }, + "step": "buy_quantity_exercise" + }, + { + "get_solution": "program", + "page": "Creating Key-Value Pairs", + "program": [ + "def buy_quantity(quantities, item, quantity):", + " quantities[item] = quantity", + "", + "def test():", + " quantities = {}", + " for _ in range(3):", + " print('What would you like to buy?')", + " item = input()", + "", + " print('How many?')", + " quantity = int(input())", + "", + " buy_quantity(quantities, item, quantity)", + "", + " print(\"OK, here's your cart so far:\")", + " print(quantities)", + "", + "test()" + ], + "response": { + "passed": true, + "result": [ + { + "text": "What would you like to buy?\n", + "type": "stdout" + }, + { + "text": "", + "type": "input_prompt" + }, + { + "text": "\n", + "type": "stdout" + }, + { + "text": "How many?\n", + "type": "stdout" + }, + { + "text": "", + "type": "input_prompt" + }, + { + "text": "\n", + "type": "stdout" + }, + { + "text": "OK, here's your cart so far:\n{'apple': 3}\nWhat would you like to buy?\n", + "type": "stdout" + }, + { + "text": "", + "type": "input_prompt" + }, + { + "text": "\n", + "type": "stdout" + }, + { + "text": "How many?\n", + "type": "stdout" + }, + { + "text": "", + "type": "input_prompt" + }, + { + "text": "\n", + "type": "stdout" + }, + { + "text": "OK, here's your cart so far:\n{'apple': 3, 'banana': 5}\nWhat would you like to buy?\n", + "type": "stdout" + }, + { + "text": "", + "type": "input_prompt" + }, + { + "text": "\n", + "type": "stdout" + }, + { + "text": "How many?\n", + "type": "stdout" + }, + { + "text": "", + "type": "input_prompt" + }, + { + "text": "\n", + "type": "stdout" + }, + { + "text": "OK, here's your cart so far:\n{'apple': 3, 'banana': 5, 'cat': 2}\n", + "type": "stdout" + } + ] + }, + "step": "buy_quantity_input_test" + }, + { + "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": "Creating Key-Value Pairs", + "program": [ + "def swap_keys_values(d):", + " new_dict = {}", + " for key in d:", + " new_dict[d[key]] = key", + " return new_dict", + "", + "print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'}))", + "print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'}))" + ], + "response": { + "passed": true, + "result": [ + { + "text": "{'pomme': 'apple', 'avocat': 'lawyer'}\n{'pomme': 'apple', 'avocat': 'avocado'}\n", + "type": "stdout" + } + ] + }, + "step": "avocado_or_lawyer" } ] \ No newline at end of file diff --git a/tests/test_steps.py b/tests/test_steps.py index 7f1443ad..12d0a4da 100644 --- a/tests/test_steps.py +++ b/tests/test_steps.py @@ -97,6 +97,10 @@ def normalise_response(response, is_message, substep): message_sections = response.pop("message_sections") if not is_message: + if message_sections: + for section in message_sections: + for message in section['messages']: + print(message) assert not message_sections else: section = message_sections[0] diff --git a/translations/english.po b/translations/english.po index a792c24a..da65d6e6 100644 --- a/translations/english.po +++ b/translations/english.po @@ -1205,6 +1205,29 @@ msgstr "\"Nope!\"" msgid "code_bits.\"OK\"" msgstr "\"OK\"" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. print(quantities) +#. +#. test() +msgid "code_bits.\"OK, here's your cart so far:\"" +msgstr "\"OK, here's your cart so far:\"" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.StringMethodsUnderstandingMutation.steps.string_lower_upper #. #. sentence = "Python rocks!" @@ -2512,6 +2535,29 @@ msgstr "'Hello there'" msgid "code_bits.'Hello'" msgstr "'Hello'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. print(quantities) +#. +#. test() +msgid "code_bits.'How many?'" +msgstr "'How many?'" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IfAndElse.steps.first_if_else #. #. condition = True @@ -2703,6 +2749,29 @@ msgstr "'This'" msgid "code_bits.'Type your name, then press Enter:'" msgstr "'Type your name, then press Enter:'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. print(quantities) +#. +#. test() +msgid "code_bits.'What would you like to buy?'" +msgstr "'What would you like to buy?'" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.BasicForLoopExercises.steps.loop_exercise_1 #. #. name = 'World' @@ -3185,6 +3254,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 +3305,66 @@ msgstr "'aeiou'" msgid "code_bits.'apfel'" msgstr "'apfel'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +#. +#. ------ +#. +#. 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.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -3400,6 +3544,32 @@ msgstr "'apple'" msgid "code_bits.'are'" msgstr "'are'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +msgid "code_bits.'avocado'" +msgstr "'avocado'" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +msgid "code_bits.'avocat'" +msgstr "'avocat'" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.TheEqualityOperator.steps.introducing_equality #. #. print(1 + 2 == 3) @@ -3408,6 +3578,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 +3656,104 @@ msgstr "'bc'" msgid "code_bits.'boite'" msgstr "'boite'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +#. +#. quantities = {} +#. quantities['dog'] = 500 +#. 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.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -3582,6 +3877,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 +4026,77 @@ msgstr "'de'" msgid "code_bits.'def'" msgstr "'def'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +#. +#. quantities = {} +#. quantities['dog'] = 500 +#. 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.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -4102,6 +4476,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): @@ -4168,6 +4557,19 @@ msgstr "'kasten'" msgid "code_bits.'kesha'" msgstr "'kesha'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +msgid "code_bits.'lawyer'" +msgstr "'lawyer'" + #. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingLists.steps.strings_sum #. #. words = ['This', 'is', 'a', 'list'] @@ -4220,6 +4622,46 @@ msgstr "'list'" msgid "code_bits.'on'" msgstr "'on'" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +#. +#. ------ +#. +#. 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): @@ -4374,103 +4816,159 @@ msgstr "'you'" msgid "code_bits.Hello" msgstr "Hello" -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.keys_are_iterable.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text #. -#. __program_indented__ +#. 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}, +#. ) #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.nested_dictionaries.text +#. assert_equal( +#. total_cost_per_item({'dog': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +msgid "code_bits.___" +msgstr "___" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.EqualsVsIs.steps.two_separate_lists.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.FunctionsAndMethodsForLists.steps.append_vs_concatenate.text +#. 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.GettingElementsAtPosition.steps.printing_the_range.text +#. 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.GettingElementsAtPosition.steps.using_len_first_time.text +#. 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.Indentation.steps.one_indented_line.text +#. 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.Indentation.steps.two_indented_lines.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.keys_are_iterable.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.InteractiveProgramsWithInput.steps.first_input.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.nested_dictionaries.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingBirdseye.steps.birdseye_loop_example.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.EqualsVsIs.steps.two_separate_lists.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingBirdseye.steps.first_birdseye_example.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.FunctionsAndMethodsForLists.steps.append_vs_concatenate.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingDictionaries.steps.first_dict.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.GettingElementsAtPosition.steps.printing_the_range.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingElif.steps.dna_example.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.GettingElementsAtPosition.steps.using_len_first_time.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingFstrings.steps.concatenate_string_number.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.Indentation.steps.one_indented_line.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingNotPage.steps.NotTrueOrTrue.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.Indentation.steps.two_indented_lines.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingOr.steps.InputAliceBob.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.InteractiveProgramsWithInput.steps.first_input.text #. #. __program_indented__ #. #. ------ #. -#. https://poeditor.com/projects/view_terms?id=490053&search=pages.MakingTheBoard.steps.naive_make_board.text +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingBirdseye.steps.birdseye_loop_example.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingBirdseye.steps.first_birdseye_example.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingDictionaries.steps.first_dict.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingElif.steps.dna_example.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingFstrings.steps.concatenate_string_number.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingNotPage.steps.NotTrueOrTrue.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.IntroducingOr.steps.InputAliceBob.text +#. +#. __program_indented__ +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.MakingTheBoard.steps.naive_make_board.text #. #. __program_indented__ #. @@ -4857,6 +5355,69 @@ msgstr "all_numbers" #. #. ------ #. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. 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.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart.text #. #. def total_cost(quantities, prices): @@ -6422,6 +6983,52 @@ 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, item, quantity): +#. quantities[item] = quantity +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. 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 +7073,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): @@ -8612,6 +9245,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 +10986,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 +11070,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 +11135,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,6 +11598,82 @@ msgstr "is_friend" msgid "code_bits.is_valid_percentage" msgstr "is_valid_percentage" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. 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.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart #. #. def total_cost(quantities, prices): @@ -11076,6 +11887,30 @@ msgstr "joined_row" msgid "code_bits.joined_rows" msgstr "joined_rows" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +#. +#. ------ +#. +#. 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 +12170,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 +12954,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 +13548,30 @@ msgstr "middle" msgid "code_bits.name" msgstr "name" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +#. +#. ------ +#. +#. 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' @@ -14640,6 +15532,36 @@ 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.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart #. #. def total_cost(quantities, prices): @@ -15268,32 +16190,117 @@ msgstr "printed" msgid "code_bits.quadruple" msgstr "quadruple" -#. 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.CreatingKeyValuePairs.steps.buy_quantity_exercise #. -#. def total_cost(quantities, prices): -#. result = 0 -#. for item in quantities: -#. price = prices[item] -#. quantity = quantities[item] -#. result += price * quantity -#. return result +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity #. #. ------ #. -#. 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.CreatingKeyValuePairs.steps.buy_quantity_exercise.text #. -#. def total_cost(quantities, prices): -#. result = 0 -#. for item in ...: -#. price = prices[item] -#. quantity = quantities[item] -#. result += price * quantity -#. return result +#. def buy_quantity(quantities, item, quantity): +#. ... #. -#. assert_equal( -#. total_cost( -#. {'dog': 5000000, 'box': 2}, -#. {'apple': 2, 'box': 5, 'cat': 100, 'dog': 100}, +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. print(quantities) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.dict_assignment_valid +#. +#. quantities = {} +#. quantities['dog'] = 500 +#. 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.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. ------ +#. +#. 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, #. ) @@ -15363,6 +16370,52 @@ msgstr "quadruple" msgid "code_bits.quantities" msgstr "quantities" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_exercise.text +#. +#. def buy_quantity(quantities, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. print(quantities) +#. +#. test() +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.DictionaryKeysAndValues.steps.cleanup_shopping_cart #. #. def total_cost(quantities, prices): @@ -15581,6 +16634,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): @@ -17961,6 +19020,48 @@ msgstr "super_secret_number" msgid "code_bits.surround" msgstr "surround" +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.avocado_or_lawyer +#. +#. def swap_keys_values(d): +#. new_dict = {} +#. for key in d: +#. new_dict[d[key]] = key +#. return new_dict +#. +#. print(swap_keys_values({'apple': 'pomme', 'avocado': 'avocat', 'lawyer': 'avocat'})) +#. print(swap_keys_values({'apple': 'pomme', 'lawyer': 'avocat', 'avocado': 'avocat'})) +#. +#. ------ +#. +#. 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 +19076,45 @@ 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, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.buy_quantity_input_test +#. +#. def buy_quantity(quantities, item, quantity): +#. quantities[item] = quantity +#. +#. def test(): +#. quantities = {} +#. for _ in range(3): +#. print('What would you like to buy?') +#. item = input() +#. +#. print('How many?') +#. quantity = int(input()) +#. +#. buy_quantity(quantities, item, quantity) +#. +#. print("OK, here's your cart so far:") +#. print(quantities) +#. +#. test() +#. +#. ------ +#. #. https://poeditor.com/projects/view_terms?id=490053&search=pages.MakingTheBoard.steps.final_text.text #. #. def make_board(size): @@ -18434,6 +19574,66 @@ 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 +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +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 +#. +#. ------ +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text +#. +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +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 +19726,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 +22694,733 @@ msgstr "" msgid "pages.CombiningCompoundStatements.title" msgstr "Combining Compound Statements" +#. 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.avocado_or_lawyer.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" +"For example, 'avocat' in French can mean either 'avocado' and 'lawyer'. So it's easy to translate\n" +"'avocado' from English to French, but it's not so clear how to translate 'avocat' back to English.\n" +"Try to guess what the following code will print:\n" +"\n" +" __copyable__\n" +"__code0__" + +#. 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 body of `buy_quantity` only needs one simple line of code." + +#. 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 "It's similar to some of the lines in the previous step, but with variables instead of hardcoded values." + +#. 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.10.text" +msgstr "Replace `'dog'` with `item`." + +#. 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.11.text" +msgstr "Replace `500` with `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.12.text" +msgstr "Don't use `'item'` or `'quantity'`, use the variables `item` and `quantity` directly." + +#. 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 "Be careful with quotes!" + +#. 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 "`'dog'` is an example of a value for `item`." + +#. 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 "What would be an example of a value for `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.5.text" +msgstr "For `'dog'`, it was `500` above." + +#. 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 "You're making a generic version of `quantities['dog'] = 500`." + +#. 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.7.text" +msgstr "The `quantities` part is fine as is." + +#. 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.8.text" +msgstr "Also keep the `[]` and `=`." + +#. 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.9.text" +msgstr "Remember that `item` is a variable, `'item'` is a string literal." + +#. 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, item, quantity): +#. ... +#. +#. def test(): +#. quantities = {} +#. buy_quantity(quantities, 'dog', 500) +#. assert_equal(quantities, {'dog': 500}) +#. buy_quantity(quantities, 'box', 2) +#. assert_equal(quantities, {'dog': 500, 'box': 2}) +#. +#. test() +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27box%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27dog%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.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.quantity +#. +#. 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. Whether the customer says they want 500 or 5 million dogs,\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, item, quantity)` which adds a new key-value pair to the `quantities` dictionary.\n" +"Here's some starting code:\n" +"\n" +" __copyable__\n" +"__code0__\n" +"\n" +"Note that `buy_quantity` should *modify* the dictionary that's passed in, and doesn't need to `return` or `print` anything.\n" +"You can assume that `item` isn't already in `quantities`." + +#. 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.buy_quantity_input_test.text" +msgstr "" +"Well done! Try it out interactively:\n" +"\n" +" __copyable__\n" +"__code0__\n" +"\n" +"Note the `int(input())` part, because `input()` returns a string, and `quantity` should be an integer\n" +"(a whole number). This'll break if you enter something that isn't a number, but that's OK for now." + +#. 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.0" +msgstr "{'dog': 500, '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.1" +msgstr "{'dog': 2, 'box': 500}" + +#. 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 "{2: 'dog', 500: 'box'}" + +#. 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 "{500: 'dog', 2: 'box'}" + +#. 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" +"__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 "" +"The result depends on the order of the keys in the original dictionary!\n" +"If you're not sure why, try running the code with `snoop` or another debugger.\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://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_append_reminder.output_prediction_choices.1" +msgstr "['dog']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_append_reminder.output_prediction_choices.2" +msgstr "['box']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_append_reminder.output_prediction_choices.3" +msgstr "['dog', 'box']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_append_reminder.output_prediction_choices.4" +msgstr "['box', 'dog']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_append_reminder.output_prediction_choices.5" +msgstr "['dog', 'dog']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_append_reminder +msgid "pages.CreatingKeyValuePairs.steps.list_append_reminder.output_prediction_choices.6" +msgstr "['box', 'box']" + +#. 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://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +msgid "pages.CreatingKeyValuePairs.steps.list_assign_invalid.output_prediction_choices.1" +msgstr "['dog']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +msgid "pages.CreatingKeyValuePairs.steps.list_assign_invalid.output_prediction_choices.2" +msgstr "['box']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +msgid "pages.CreatingKeyValuePairs.steps.list_assign_invalid.output_prediction_choices.3" +msgstr "['dog', 'box']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +msgid "pages.CreatingKeyValuePairs.steps.list_assign_invalid.output_prediction_choices.4" +msgstr "['box', 'dog']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +msgid "pages.CreatingKeyValuePairs.steps.list_assign_invalid.output_prediction_choices.5" +msgstr "['dog', 'dog']" + +#. https://poeditor.com/projects/view_terms?id=490053&search=pages.CreatingKeyValuePairs.steps.list_assign_invalid +msgid "pages.CreatingKeyValuePairs.steps.list_assign_invalid.output_prediction_choices.6" +msgstr "['box', 'box']" + +#. 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 "['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 "['dog', '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 "['box', 'dog']" + +#. 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.3" +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.4" +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.5" +msgstr "['cat', '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.6" +msgstr "['box', 'dog', '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.7" +msgstr "['dog', '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.8" +msgstr "['dog', 'cat', 'box']" + +#. 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 dictionary and fill it with key-value pairs depending on the two input dictionaries." + +#. 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 "You've seen code that does this before." + +#. 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.10.text" +msgstr "The values should be German words, so they should come from the second 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.11.text" +msgstr "Specifically the keys of your dictionary should be the keys of the first 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.12.text" +msgstr "And the values of your dictionary should be the values of the second 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.13.text" +msgstr "What about the values of the first dictionary and the keys of the second dictionary? They're important." + +#. 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.14.text" +msgstr "Look at the French words `'pomme'` and `'boite'` in the example test." + +#. 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.15.text" +msgstr "The values of the first input dictionary are the keys of the second input 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 "Specifically the previous step. The overall structure you want is similar to `total_cost_per_item`." + +#. 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 "Start by creating a new empty 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.4.text" +msgstr "Return the dictionary at the end. Then fill in the code in between." + +#. 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 "You need a `for` loop." + +#. 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 "The line `totals[item] = quantities[item] * prices[item]` from the previous step is close to what you need." + +#. 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.7.text" +msgstr "You don't need to multiply anything with `*`, the names are different, and there's another difference in logic." + +#. 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.8.text" +msgstr "Think about what the keys and values of the new dictionary should be." + +#. 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.9.text" +msgstr "The keys should be English words, so they should come from the first 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 "Don't modify 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.1.text" +msgstr "You need to create a new dictionary and fill it with key-value pairs depending on the input dictionary." + +#. 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 "" +"You've done this in the previous exercise. The overall structure you want is similar to `make_english_to_german`." + +#. 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 "It's actually even simpler, it just might feel weird." + +#. 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 "Start by creating a new empty dictionary. Return the dictionary at the end. Put a `for` loop in between." + +#. 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 "Think about what the keys and values of the new dictionary should be." + +#. 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.6.text" +msgstr "There's only one possible thing for you to loop over." + +#. 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.7.text" +msgstr "Use each key in the input dictionary `d` to get the corresponding value." + +#. 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! Let's use an English-to-French dictionary to create a French-to-English dictionary.\n" +"Write a function which takes a dictionary returns a new dictionary where the keys and values are swapped,\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 only need to fill in the `___` part." + +#. 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 "But if you want, you could also just put a variable name there, and then add a new line below it." + +#. 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.10.text" +msgstr "The dictionary is the thing that this function creates, builds, and returns." + +#. 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.11.text" +msgstr "i.e. `totals`." + +#. 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.12.text" +msgstr "Note how `'apple'` is a key in all three dictionaries in that test." + +#. 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.13.text" +msgstr "i.e. the dictionaries `quantities`, `prices`, and `totals`." + +#. 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 "Look at the tests with `assert_equal`. In the first example, the expected output is `{'apple': 6}`. Why?" + +#. 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 "Because the customer bought 2 apples, and each apple costs 3, so the total cost is `2 * 3 = 6`." + +#. 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 "" +"The `'box': 5` part is ignored because the customer didn't buy any boxes. It just means that the price of a box is 5." + +#. 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 "You need to add a new key-value pair to a 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.6.text" +msgstr "Identify the dictionary, the key, and the value." + +#. 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.7.text" +msgstr "They are all present in the given code already." + +#. 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.8.text" +msgstr "The value is the total cost for that item, which is the quantity multiplied by the 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.9.text" +msgstr "i.e. `quantities[item] * prices[item]`." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.requirements" +msgstr "Run the program above, but replace the `___` with the correct code." + +#. https://futurecoder.io/course/#CreatingKeyValuePairs +#. +#. # __code0__: +#. 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': 500, 'box': 2}, {'dog': 100, 'box': 5}), +#. {'dog': 50000, 'box': 10}, +#. ) +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.%27apple%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.%27dog%27 +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.___ +#. +#. 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.prices +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.quantities +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.total_cost_per_item +#. +#. https://poeditor.com/projects/view_terms?id=490053&search=code_bits.totals +msgid "pages.CreatingKeyValuePairs.steps.total_cost_per_item_exercise.text" +msgstr "" +"Thanks for shopping with us! Let's see how much you just spent on each item.\n" +"\n" +"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" +"__code0__" + +#. 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 35138154..f6e8d8af 100644 Binary files a/translations/locales/en/LC_MESSAGES/futurecoder.mo and b/translations/locales/en/LC_MESSAGES/futurecoder.mo differ