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!")