Integer to roman numeral ======================== .. contents:: :local: .. include:: roman_numeral_introduction.rst Exercise -------- Approach ^^^^^^^^ Create a ``dict`` with the 7 single-letter and the 6 double-letter symbols sorted with *decending* values. Iterate over the ``dict`` and .. interactive_code_block:: :caption: Complete the function that converts an integer to a roman numeral def integer_to_roman_numeral(integer): """Convert an integer to a roman numeral.""" # Create a dict with the roman symbols and their integer values roman_symbols = { 'M':1000, 'CM':900, 'D':500, 'CD':400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1 } # Create a variable to be able to add the roman symbols roman_numeral = '' # Substract the values of the roman symbols as long as the integer is greater then 0 while #.......... # Iterate over the dict with the roman symbols for #.......... # Calculate how many times the roman symbol has to be added repetitions = #.......... # Add the symbol(s) to the result roman_numeral += #.......... # Substract the values from the integer integer -= #.......... return roman_numeral # Create a dict with test values test_values = {'CXXIII': 123, 'CCCXXI':321, 'CDXLIV':444, 'CMXCIX': 999, 'MMXXI': 2021} # Print a table with test values and the result of the function print(' Test values | Result') print('----------------+-------') for roman_numeral, integer in test_values.items(): print('{:6} | {:>6} | {:>6}'.format(integer, roman_numeral, integer_to_roman_numeral(integer))) Know how ^^^^^^^^ Iterating over a dict """"""""""""""""""""" Though the data type of a ``dict`` is a *mapping* and not a *sequence* like ``str``, ``tuple`` and ``list`` it can be seen as a sequence of key / value pairs. The most noticable difference between a mapping and a sequence is that a ``dict``'s value can *only* be accessed by its key and not by its index. Since Python 3.7 the order is even predictable (new elements will be appended at the end). .. interactive_code_block:: :caption: Iterate over the keys of the dict roman_symbols = { 'M':1000, 'CM':900, 'D':500, 'CD':400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1 } # Iterate over the keys of the dict for roman_symbol in roman_symbols: print(roman_symbol) Since the solution above only returns the keys of the dict an extra step is required to get the values as well. .. interactive_code_block:: :caption: Iterate over the keys of the dict and get the values roman_symbols = { 'M':1000, 'CM':900, 'D':500, 'CD':400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1 } # Iterate over the keys of the dict for roman_symbol in roman_symbols: # Get the value for the key value = roman_symbols[roman_symbol] print(roman_symbol, value) An alternative solution is to use the ``.items()`` method of the ``dict`` class. .. interactive_code_block:: :caption: Iterate over the keys and values of the dict roman_symbols = { 'M':1000, 'CM':900, 'D':500, 'CD':400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1 } # Iterate over the keys and values of the dict for roman_symbol, value in roman_symbols.items(): print(roman_symbol, value) Go to :ref:`solution `.