# Day 18: Exercise Solutions Here are our solutions for the day 18 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!

### 1) Import the `fractions` module and create a `Fraction` from the float `2.25`.

First things first, we need to import the `fractions` module. I'm just going to use a regular import in this case:

``````import fractions
``````

If we look at the documentation for the `fractions` module, we can see that we can create a `Fraction` in many different ways.

Right at the top of the relevant documentation we have this signature that describes the various ways we can create a `Fraction`. There are also a tonne of examples a little further down.

The signature looks like this:

``````fractions.Fraction(numerator=0,  denominator=1)
fractions.Fraction(other_fraction)
fractions.Fraction(float)
fractions.Fraction(decimal)
fractions.Fraction(string)
``````

As we can see, one of the options is to just pass in a single float value, and `Fraction` will take care of the rest.

``````import fractions

fraction = fractions.Fraction(2.25)
``````

If we print `fraction` we now get something like this:

``````9/4
``````

### 2) Import only the `fsum` function from the `math` module and use it to find the sum of the following series of floats.

``````numbers = [1.43, 1.1, 5.32, 87.032, 0.2, 23.4]
``````

For this exercise we need to do a specific import, so we're going to be using the `from ... import ...` syntax. In this instance we want to import from the `math` module, and what we want to import is `fsum`.

``````from math import fsum
``````

Now that we have the `fsum` function, we can just pass our list of numbers to it, since `fsum`, like `sum`, accepts an iterable.

``````from math import fsum

numbers = [1.43, 1.1, 5.32, 87.032, 0.2, 23.4]
fsum(numbers)
``````

You can print the result if you want verify that everything worked. If my math is right, answer should be `118.482`.

### 3) Import the `random` module using an alias, and find a random number between `1` and `100` using the `randint` function.

Once again, we can find all the information we need in the documentation. As we can see from the function signature, `randint` takes in two parameters, `a` and `b`, which determine the range of numbers it can choose from.

One very important thing we need to pay attention to is here:

Return a random integer N such that a <= N <= b.

Note that `N` can be equal to `b`, so that means the stop value is inclusive, unlike with `range`.

Now that we know how to use `randint`, we need to import `random` using an alias. I'm going to use `rand` in this case.

``````import random as rand
``````

Now we can call `randint` using `rand.randint()`.

``````import random as rand

print(rand.randint(1, 100))
``````

### 4) Use the `randint` function from the exercise above to create a new version of the guessing game we made in day 8. This time the program should generate a random number, and you should tell the user whether their guess was too high, or too low, until they get the right number.

I'm going to cheat a little bit and just copy across the solution I wrote from day 8.

``````target_number = 47

guess = int(input("Enter a number: "))

while guess != target_number
print("Wrong!")
guess = int(input("Enter a number: "))

print("You guessed correctly!")
``````

The only real change we need to make here is to change `target_number` from a static integer to a dynamic value generated by `randint`.

This is a simple modification that makes the game a great deal more fun!

``````import random as rand

target_number = rand.randint(1, 100)

guess = int(input("Enter a number: "))

while guess != target_number
print("Wrong!")
guess = int(input("Enter a number: "))

print("You guessed correctly!")
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.82567286,"math_prob":0.98758525,"size":3522,"snap":"2023-14-2023-23","text_gpt3_token_len":880,"char_repetition_ratio":0.1421262,"word_repetition_ratio":0.0660066,"special_character_ratio":0.2524134,"punctuation_ratio":0.14209116,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99894136,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-29T16:11:48Z\",\"WARC-Record-ID\":\"<urn:uuid:acc9e853-17a2-43ac-91ad-50d9fe9b0343>\",\"Content-Length\":\"139604\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6fec25fd-8064-499a-824d-a6fc91a62ad3>\",\"WARC-Concurrent-To\":\"<urn:uuid:d109bb5f-19f6-4507-b109-e365d2fa1d87>\",\"WARC-IP-Address\":\"216.24.57.1\",\"WARC-Target-URI\":\"https://teclado.com/30-days-of-python/python-30-day-18-exercise-solutions/\",\"WARC-Payload-Digest\":\"sha1:RWESVIQP7OZVDNPETDNQ3PWCNWITXOHO\",\"WARC-Block-Digest\":\"sha1:L7WDB6OWMJYGDGEOA6IXSN2CCSYYXAJQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296949009.11_warc_CC-MAIN-20230329151629-20230329181629-00081.warc.gz\"}"}