Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Get a Random Entry from a Python Dictionary

#1
How to Get a Random Entry from a Python Dictionary

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;669667&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">This article will show you how to get a random entry from a Dictionary in Python.</p>
<p>To make it more interesting, we have the following running scenario:</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f468-200d-1f3eb.png" alt="?‍?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>The Plot</strong>: <em>Mr. Sinclair, an 8th great Science Teacher, is giving his students a quiz on the first 25 <em><a rel="noreferrer noopener" href="https://ptable.com/?lang=en#Properties" target="_blank">Periodic Table</a></em> elements. He has asked you to write a Python script so that when run, it generates a random key, value, or key:value pair from the Dictionary shown below to ask his students.</em></p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">els = {'Hydrogen': 'H', 'Helium': 'He', 'Lithium': 'Li', 'Beryllium': 'Be', 'Boron': 'B', 'Carbon': 'C', 'Nitrogen': 'N', 'Oxygen': 'O', 'Fluorine': 'F', 'Neon': 'Ne', 'Sodium': 'Na', 'Magnesium': 'Mg', 'Aluminum': 'Al', 'Silicon': 'Si', 'Phosphorus': 'P', 'Sulfur': 'S', 'Chlorine': 'Cl', 'Argon': 'Ar', 'Potassium': 'K', 'Calcium': 'Ca', 'Scandium': 'Sc', 'Titanium': 'Ti', 'Vanadium': 'V', 'Chromium': 'Cr', 'Manganese': 'Mn'}</pre>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to get a random entry from a Dictionary<em>?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-items-method/" data-type="URL" data-id="https://blog.finxter.com/python-dict-items-method/" target="_blank"><code>items()</code></a></li>
<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-keys-method/" data-type="URL" data-id="https://blog.finxter.com/python-dict-keys-method/" target="_blank"><code>keys()</code></a></li>
<li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a><a href="https://blog.finxter.com/python-random-module/"> </a>and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-values-method/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-values-method/" target="_blank"><code>values()</code></a></li>
<li><strong>Method 4</strong>: Use <a rel="noreferrer noopener" href="https://docs.python.org/3/library/random.html" data-type="URL" data-id="https://docs.python.org/3/library/random.html" target="_blank"><code>sample()</code></a></li>
<li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-seed-a-deep-dive/" data-type="URL" data-id="https://blog.finxter.com/python-random-seed-a-deep-dive/" target="_blank"><code>np.random.choice()</code></a></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Preparation</h2>
<p>This article uses the <a href="https://blog.finxter.com/python-random-module/"><code>random</code></a> library for each example. For these code samples to run error-free, add the following snippet to the top of each example.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import random</pre>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 1: Use random.choice() and items()</h2>
<p class="has-global-color-8-background-color has-background">This example uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-items-method/" data-type="URL" data-id="https://blog.finxter.com/python-dict-items-method/" target="_blank"><code>items()</code></a> to generate a random <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> <em><strong>key:value</strong></em> pair.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">el_list = list(els.items())
random_el = random.choice(el_list)
print(random_el)</pre>
<p>The above code converts the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> of <a rel="noreferrer noopener" href="https://ptable.com/?lang=en#Properties" data-type="URL" data-id="https://ptable.com/?lang=en#Properties" target="_blank">Periodic Table</a> Elements to a <a rel="noreferrer noopener" href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="URL" data-id="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" target="_blank">List of Tuples</a> and saves it to <code>el_list</code>. If output to the terminal, the contents of <code>el_list</code> contains the following.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[('Hydrogen', 'H'), ('Helium', 'He'), ('Lithium', 'Li'), ('Beryllium', 'Be'), ('Boron', 'B'), ('Carbon', 'C'), ('Nitrogen', 'N'), ('Oxygen', 'O'), ('Fluorine', 'F'), ('Neon', 'Ne'), ('Sodium', 'Na'), ('Magnesium', 'Mg'), ('Aluminum', 'Al'), ('Silicon', 'Si'), ('Phosphorus', 'P'), ('Sulfur', 'S'), ('Chlorine', 'Cl'), ('Argon', 'Ar'), ('Potassium', 'K'), ('Calcium', 'Ca'), ('Scandium', 'Sc'), ('Titanium', 'Ti'), ('Vanadium', 'V'), ('Chromium', 'Cr'), ('Manganese', 'Mn')]</pre>
<p>Next, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> is called and passed one (1) argument: <code>el_list</code>. </p>
<p>The results return a random <code><a rel="noreferrer noopener" href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="URL" data-id="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" target="_blank">Tuple</a> </code>from the <a rel="noreferrer noopener" href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="URL" data-id="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" target="_blank">List of Tuples</a>, saves to <code>random_el </code>and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>('Oxygen', 'O')</code></td>
</tr>
</tbody>
</table>
</figure>
<p>This code can be streamlined down to the following.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">random_el = random.choice(list(els.items()))</pre>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-get-a-random-entry-from-a-dictionary/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FLtYPiOZq-BQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 2: Use random.choice() and keys()</h2>
<p class="has-global-color-8-background-color has-background">This example uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-keys-method/" data-type="URL" data-id="https://blog.finxter.com/python-dict-keys-method/" target="_blank"><code>keys()</code></a> to generate a random <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> <em><strong>key</strong></em>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">random_el = random.choice(list(els.keys()))
print(random_el)</pre>
<p>The above code calls <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> and passes it one (1) argument: the keys of the <code>els</code> <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> converted to a <a rel="noreferrer noopener" href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="URL" data-id="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" target="_blank">List of Tuples</a>.</p>
<p>The result returns a random <strong><em>key</em></strong>, saves to <code>random_el </code>and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>Beryllium</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-get-a-random-entry-from-a-dictionary/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqSEEOOTUrjk%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 3: Use random.choice() and dict.values()</h2>
<p class="has-global-color-8-background-color has-background">This example uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-values-method/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-values-method/" target="_blank"><code>values()</code></a> to generate a random <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> <em><strong>value</strong></em>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">random_el = random.choice(list(els.values()))
print(random_el)</pre>
<p>The above code calls <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random.choice()</code></a> and passes it one (1) argument: the keys of the <code>els</code> <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> converted to a <a rel="noreferrer noopener" href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="URL" data-id="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" target="_blank"><code>List of Tuples</code></a>.</p>
<p>The result returns a random <strong><em>value</em></strong>, saves to <code>random_el </code>and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>Si</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-get-a-random-entry-from-a-dictionary/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FENjl8yKu00M%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 4: Use sample()</h2>
<p class="has-global-color-8-background-color has-background">This example uses the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/random.html" data-type="URL" data-id="https://docs.python.org/3/library/random.html" target="_blank"><code>sample()</code></a> function to generate a random <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> <em><strong>key</strong></em>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from random import sample
random_el = sample(list(els), 1)
print(random_el)</pre>
<p>The above code requires <a rel="noreferrer noopener" href="https://docs.python.org/3/library/random.html" data-type="URL" data-id="https://docs.python.org/3/library/random.html" target="_blank"><code>sample</code></a> to be imported from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-module/" data-type="URL" data-id="https://blog.finxter.com/python-random-module/" target="_blank"><code>random</code></a> library.</p>
<p>Then, <a rel="noreferrer noopener" href="https://docs.python.org/3/library/random.html" data-type="URL" data-id="https://docs.python.org/3/library/random.html" target="_blank"><code>sample()</code></a> is called and passed two (2) arguments: <code>els</code> converted to a <a rel="noreferrer noopener" href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="URL" data-id="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" target="_blank"><code>List of Tuples</code></a> and the number of random <strong><em>keys</em></strong> to return. </p>
<p>The results save to <code>random_el</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>['Carbon']</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 5: Use np.random.choice()</h2>
<p class="has-global-color-8-background-color has-background">This example uses NumPy and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-seed-a-deep-dive/" data-type="URL" data-id="https://blog.finxter.com/python-random-seed-a-deep-dive/" target="_blank"><code>np.random.choice()</code></a> to generate a random <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> <em><strong>key</strong></em>.</p>
<p>Before moving forward, please ensure the <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy</a> library is installed. Click <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-numpy-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-numpy-in-python/" target="_blank">here</a> if you require instructions.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np random_el = np.random.choice(list(els), 1)
print(random_el)</pre>
<p>This code imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy</a> library installed above.</p>
<p>Then, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-seed-a-deep-dive/" data-type="URL" data-id="https://blog.finxter.com/python-random-seed-a-deep-dive/" target="_blank"><code>np.random.choice()</code></a> is called and passed two (2) arguments: <code>els</code> converted to a <a rel="noreferrer noopener" href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="URL" data-id="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" target="_blank">List of Tuples</a> and the number of random <strong><em>keys</em></strong> to return. </p>
<p>The results save to <code>random_el</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>['Chromium' 'Silicon' 'Oxygen']</code></td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: <a rel="noreferrer noopener" href="https://docs.python.org/3/library/random.html" data-type="URL" data-id="https://docs.python.org/3/library/random.html" target="_blank"></a><a rel="noreferrer noopener" href="https://blog.finxter.com/python-random-seed-a-deep-dive/" data-type="URL" data-id="https://blog.finxter.com/python-random-seed-a-deep-dive/" target="_blank"><code>np.random.choice()</code></a> has an additional parameter that can be passed. This parameter is a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> containing associated probabilities.</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Bonus:</h2>
<p class="has-global-color-8-background-color has-background">This code generates a random <em>key:value</em> pair from a list of tuples. When the teacher runs this code, a random question displays on the screen and waits for a student to answer. Press 1 to display the answer, 2 to quit.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import keyboard
import random
import time els = {'Hydrogen': 'H', 'Helium': 'He', 'Lithium': 'Li', 'Beryllium': 'Be', 'Boron': 'B', 'Carbon': 'C', 'Nitrogen': 'N', 'Oxygen': 'O', 'Fluorine': 'F', 'Neon': 'Ne', 'Sodium': 'Na', 'Magnesium': 'Mg', 'Aluminum': 'Al', 'Silicon': 'Si', 'Phosphorus': 'P', 'Sulfur': 'S', 'Chlorine': 'Cl', 'Argon': 'Ar', 'Potassium': 'K', 'Calcium': 'Ca', 'Scandium': 'Sc', 'Titanium': 'Ti', 'Vanadium': 'V', 'Chromium': 'Cr', 'Manganese': 'Mn'} print('1 Answer 2 quit')
def quiz(): while True: k, v = random.choice(list(els.items())) print(f'\nWhat is the Symbol for {k}?') pressed = keyboard.read_key() if pressed == '1': print(f'The answer is {v}!') elif pressed == '2': print("Exiting\n") exit(0) time.sleep(5)
quiz()</pre>
<p class="has-text-align-center has-base-3-color has-global-color-9-background-color has-text-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2728.png" alt="✨" class="wp-smiley" style="height: 1em; max-height: 1em;" />Finxter Challenge!<br />Write code to allow the teacher to enter the answer!</p>
</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Summary</h2>
<p>This article has provided five (5) ways to get a random entry from a Dictionary to select the best fit for your coding requirements.</p>
<p>Good Luck &amp; Happy Coding!</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Programmer Humor – Blockchain</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="280" height="394" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png" alt="" class="wp-image-457795" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png 280w, https://blog.finxter.com/wp-content/uplo...13x300.png 213w" sizes="(max-width: 280px) 100vw, 280px" /><figcaption><em>“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.”</em> <strong>source </strong> – <a href="https://imgs.xkcd.com/comics/blockchain.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/blockchain.png" target="_blank" rel="noreferrer noopener">xkcd</a></figcaption></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/09/...ictionary/
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016