Solutions to Introductory Projects

Hello World Solution

 1# ---------------------------------------------------------
 2#                          NOTES
 3# ---------------------------------------------------------
 4
 5# ---------------------------------------------------------
 6#                         INCLUDES
 7# ---------------------------------------------------------
 8
 9# ---------------------------------------------------------
10#                         METHODS
11# ---------------------------------------------------------
12
13
14def say_hello(target='World'):
15    """
16    Python method that says hello to a target
17
18    :param target: A target to direct your greeting. Defaults to 'World' if target is not specified
19    :type target: str
20    :return: Prints 'Hello World!' in the console.
21    :rtype: None
22    """
23    print(f"Hello {target}!")
24    return None
25
26
27# ---------------------------------------------------------
28#                          MAIN
29# ---------------------------------------------------------
30
31
32if __name__ == "__main__":
33    say_hello()
34    name = input("Who do I have the pleasure of speaking to?")
35    say_hello(name)
36
37# ---------------------------------------------------------
38#                       END OF FILE
39# ---------------------------------------------------------

Weight Calculator Solution

 1# ---------------------------------------------------------
 2#                          NOTES
 3# ---------------------------------------------------------
 4
 5# ---------------------------------------------------------
 6#                         INCLUDES
 7# ---------------------------------------------------------
 8
 9# ---------------------------------------------------------
10#                         METHODS
11# ---------------------------------------------------------
12
13planet_forces = {'Mercury': 0.38,
14                 'Venus': 0.9,
15                 'Earth': 1,
16                 'Moon': 0.17,
17                 'Mars': 0.38,
18                 'Jupiter': 2.53,
19                 'Saturn': 1.07,
20                 'Uranus': 0.89,
21                 'Neptune': 1.14
22                 }
23
24
25def print_weights(mass: float) -> None:
26    """
27    Print the weights of the object on each planet for a given mass.
28
29    :param mass: mass of the object
30    :type mass: float
31    :return: Prints weights on each of the planets and moon.
32    :rtype: None
33    """
34    print('This is your weight on each planet:')
35    for planet, force in planet_forces.items():
36        print(f'{planet}: {round(force*mass, 2)}Kg')
37
38# ---------------------------------------------------------
39#                          MAIN
40# ---------------------------------------------------------
41
42
43if __name__ == "__main__":
44    user_mass = input("Enter a mass in kilograms")
45    print_weights(float(user_mass))
46
47# ---------------------------------------------------------
48#                       END OF FILE
49# ---------------------------------------------------------

Fibonacci Sequence Solution

 1# ---------------------------------------------------------
 2#                          NOTES
 3# ---------------------------------------------------------
 4
 5# ---------------------------------------------------------
 6#                         INCLUDES
 7# ---------------------------------------------------------
 8
 9# ---------------------------------------------------------
10#                         METHODS
11# ---------------------------------------------------------
12
13sequence = {0: 0, 1: 1}
14
15
16def fibonacci(index):
17    """Calculates the fibonacci number of a given index"""
18
19    # if we have already computed this index, return it.
20    if index in sequence:
21        return sequence[index]
22
23    # Compute and store the Fibonacci number recursively
24    sequence[index] = fibonacci(index - 1) + fibonacci(index - 2)
25    return sequence[index]
26
27
28def fibonacci_list(max_index):
29    """
30    Method that implements the fibonacci sequence to a given value
31
32    :param max_index: the maximum of the for loop for the fibonacci sequence.
33    :type max_index: int
34    :return: Creates a list of max_index fibonacci numbers.
35    :rtype: list
36    """
37    return [fibonacci(index) for index in range(max_index)]
38
39# ---------------------------------------------------------
40#                          MAIN
41# ---------------------------------------------------------
42
43
44if __name__ == "__main__":
45    number = int(input("Enter a maximum number for a fibonacci sequence."))
46    fibo_list = fibonacci_list(number)
47    print(fibo_list)
48
49
50# ---------------------------------------------------------
51#                       END OF FILE
52# ---------------------------------------------------------

Fizz Buzz Solution

 1# ---------------------------------------------------------
 2#                          NOTES
 3# ---------------------------------------------------------
 4
 5# ---------------------------------------------------------
 6#                         INCLUDES
 7# ---------------------------------------------------------
 8
 9# ---------------------------------------------------------
10#                         METHODS
11# ---------------------------------------------------------
12
13
14def fizzbuzz(max_index):
15    """
16    Method that implements the fizzbuzz problem
17
18    :param max_index: the maximum of the for loop for fizzbuzz.
19    :type max_index: int
20    :return: Prints max_index amount of statements in the console.
21    :rtype: None
22    """
23    triggers = {3: 'Fizz', 5: 'Buzz'}
24
25    for index in range(1, max_index):
26        return_str = ''
27
28        for trigger, value in triggers.items():
29            if index % trigger == 0:
30                return_str += value
31
32        if return_str == '':
33            return_str = index
34
35        print(return_str)
36
37# ---------------------------------------------------------
38#                          MAIN
39# ---------------------------------------------------------
40
41
42if __name__ == "__main__":
43    number = int(input("Enter a maximum number for fizzbuzz."))
44    fizzbuzz(number)
45
46# ---------------------------------------------------------
47#                       END OF FILE
48# ---------------------------------------------------------