# Day 7: Hangman

## How to Break a Complex Problem into a Flow Chart

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MN2bq9N3xl3KynAsX6r%2F-MN2fAblqt3It92FCMjZ%2Fimage.png?alt=media\&token=fd0d7c23-e041-4c30-bd9c-d00efba8aa18)

## Picking Random Words and Checking Answers

### Step 1

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MN2bq9N3xl3KynAsX6r%2F-MN2iq5ILVWvhnkVWCKm%2Fimage.png?alt=media\&token=edb94cb5-83a2-41cf-a434-386439be8fb2)

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MN2bq9N3xl3KynAsX6r%2F-MN2ivnqRgYCGXmJdQlD%2Fimage.png?alt=media\&token=6a81526e-621c-4fac-b715-28c4822e945c)

[Help resources ](https://developers.google.com/edu/python/lists#for-and-in)

**RANDOM.SAMPLE( )**\
We used the random.sample() list previously when we created our Password Generator. To brush up on this module:\
**random.sample(\<list\_name/set>,     \<how\_many\_to\_choose\_from\_the\_list*****)***

{% hint style="info" %}
Python’s random module provides random.sample() function for random sampling and **randomly pick more than one element from the list without repeating elements**. The random.sample() returns a list of unique elements chosen randomly from the list, sequence, or set, we call it random sampling without replacement.

In simple terms, for example, you have a list of 100 names, and you want to choose ten names randomly from it without repeating names, then you must use random.sample().

**`random.sample(population, k)`**

The `random.sample()` function has two arguments, and both are required.

* The `population` can be any sequence such as list, set from which you want to select a `k` length number.
* The `k` is the number of random items you want to select from the sequence.  `k` must be less than the size of the specified list.
* A `random.sample()` function raises a type error if you miss any of the required arguments.

The `random.sample()` returns a new list containing the randomly selected item\
**Example**

**`import random`**\
**`num_list = [20, 40, 80, 100, 120]`** \
\&#xNAN;**`print ("Choosing 3 random items from a list using random.sample() function")`** \
**`new_random_list = random.sample(num_list, 3) print(sampled_list`**

**`Choosing 3 random items from a list using random.sample() function`**&#x20;

**`[40. 120, 80]`**
{% endhint %}

**RANDOM.CHOICE( ) & RANDOM.CHOICES( )**\
The main difference between 'sample' and 'choice', is that 'choice' only returns a SINGLE item from a  list UNLESS you use the random.choices( ) function.\
This can return multiple items AND CAN also return REPEATED items OR it CAN REPEAT ELEMENTS

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MN2p4sZ2EOU-VAtjUVd%2F-MN2t52RCtjVxsiu7zfX%2Fimage.png?alt=media\&token=9bdb70ba-b353-4dea-8c92-5e3e4b19705a)

{% hint style="info" %}
**`import random`**\
**`numbers = list(range(10))`**\
**`print(random.choices(numbers, 10))`**\
\
**`Output`**\
**`[3, 6, 8, 4, 2, 1, 0, 5, 9, 7]`**\
\
**`AN INTERESTING DIFFERENCE BETWEEN CHOICES AND SAMPLE (APART FROM THE ABOVE COMPARISON IS THAT WITH CHOICES YOU CAN GET MORE RETURNS THAN WHAT IS INITIALLY IN THE LIST>`**\
**`FOR EXAMPLE:`**
{% endhint %}

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MN2p4sZ2EOU-VAtjUVd%2F-MN3JKfdoDLaElgjMnWJ%2Fimage.png?alt=media\&token=a4712df8-5be6-45e5-af35-6961ba1c4757)
